有时候我们创建的窗体,不想在任务栏中显示,那么怎么做到呢? 如果以0号窗体为父窗体创建窗体那么就会在任务栏中显示,如果创建一个空的父窗体,在基于这个窗体创建自己要用的窗体,那么就不会在任务栏中显示。 基于上述原理,就开始上代码 准备回调的消息函数:
LRESULT CALLBACK MsgBack(HWND hWnd, unsigned int uMsg, _w64 unsigned int wParam, _w64 long lParam)
{
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
接下来 创建空的窗体:
HWND CreateEmptyWin()
{
wchar_t fatherClassName[] = L"fatherClass";
wchar_t fatherWinName[] = L"father测试";
WNDCLASSW wnd;
wnd.style = CS_HREDRAW | CS_VREDRAW;
wnd.lpfnWndProc = MsgBack;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.lpszMenuName = 0;
wnd.hInstance = GetModuleHandle(0);
wnd.hIcon = 0;
wnd.hCursor = 0;
wnd.hbrBackground = NULL;
wnd.lpszClassName = fatherClassName;
RegisterClassW(&wnd);
HWND emptyHwnd = CreateWindowW(
wnd.lpszClassName, fatherWinName, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |
WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
0, 0, 100, 100,
HWND_DESKTOP, 0, wnd.hInstance, 0);
return emptyHwnd;
}
创建自己要用的窗体:
HWND CreateWin(bool showInTaskbar)
{
wchar_t ClassName[] = L"Class";
wchar_t WinName[] = L"测试";
WNDCLASSW wnd;
wnd.style = CS_HREDRAW | CS_VREDRAW;
wnd.lpfnWndProc = MsgBack;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.lpszMenuName = 0;
wnd.hInstance = GetModuleHandle(0);
wnd.hIcon = LoadIcon(0, IDI_APPLICATION);
wnd.hCursor = LoadCursor(0, IDC_ARROW);
wnd.hbrBackground = NULL;
wnd.lpszClassName = ClassName;
RegisterClassW(&wnd);
HWND mySelfHwnd = CreateWindowW(
wnd.lpszClassName, WinName, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |
WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
810, 740, 300, 300,
HWND_DESKTOP, 0, wnd.hInstance, 0);
ShowWindow(mySelfHwnd, SW_SHOW);
UpdateWindow(mySelfHwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return mySelfHwnd;
}
现在自己的窗体已经有了,空窗体也有了,如何使用自己的窗体用空窗体创建呢? 主要修改下面这个部分: 既然要把红圈部分修改为空窗体,那么在创建自己要用的窗体之前,代码可以修改为: 接下来就是测试了: 当 参数 showInTaskbar 为 true时: 当 参数 showInTaskbar 为 false时:
|