作者:朱金灿
来源:clever101的专栏
为什么大多数人学不会人工智能编程?>>>
模态窗口和非模态窗口的区别模态窗口和非模态窗口的主要区别在于是独占的活动窗口。模态窗口是独占的活动窗口,非模态窗口不是独占的活动窗口,是可以和其它窗口进行切换的。注意这里说的模态窗口和非模态窗口并不限于模态对话框和非模态对话框。
创建模态窗口和非模态窗口的做法模态窗口分为两种,一种是模态对话框,使用DialogBox函数来d出,这个网上资料很多,这里就不介绍了;这里要重点介绍的是创建不是对话框界面形式的模态窗口,具体做法是需要在CreateWindow后将它的父窗口设置为不可用,使用EnableWindow函数,在关闭模态窗口之前调用EnableWindow函数将父窗口设置为可用。有两个创建模态窗口的函数:CreateChildWindow和DisplayModalWindow。建议使用DisplayModalWindow,因为使用DisplayModalWindow更为简洁。创建非模态窗口则直接调用创建窗口函数即可。
//子窗口的消息响应函数
LRESULT CALLBACK WndProc1(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
switch (wMsg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
case WM_CLOSE:
{
//启用主窗口
if(NULL!= g_hWnd)
EnableWindow(g_hWnd, TRUE);
return DefWindowProc(hWnd, wMsg, wParam, lParam);
}
default:
return DefWindowProc(hWnd, wMsg, wParam, lParam);
}
}
/*
目的:创建模态或非模态子窗口
pszWinTitle —— 窗口标题
pszClassName —— 窗口类名(这个可以自己指定一个常量字符串)
bModal —— 是否为模态窗口,true为模态,false为非模态
*/
void CreateChildWindow(LPCTSTR pszWinTitle,TCHAR* pszClassName,bool bModal)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc1;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = GetModuleHandle(nullptr);
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = (LPCWSTR)pszClassName;
wcex.hIconSm = 0;
RegisterClassEx(&wcex);
HWND hChildWnd = CreateWindow(pszClassName, pszWinTitle,WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
0, CW_USEDEFAULT, 0, NULL, NULL, wcex.hInstance, 0);
//禁用主窗口
if(bModal)
EnableWindow(g_hWnd,FALSE);
ShowWindow(hChildWnd, SW_SHOW);
UpdateWindow(hChildWnd);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK ChildWinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
//创建模态窗口的第二种做法
void DisplayModalWindow(HWND hParent)
{
// 注册窗口类
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInst;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpfnWndProc = ChildWinProc;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = TEXT("ModalWindow2");
RegisterClass(&wndclass);
EnableWindow(hParent, FALSE);
HWND hChildWin = CreateWindow(TEXT("ModalWindow2"),TEXT("模态窗口2"),WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
hParent,
NULL,
(HINSTANCE)GetWindowLong(hParent, GWLP_HINSTANCE),
NULL);
ShowWindow(hChildWin, SW_SHOW);
UpdateWindow(hChildWin);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
EnableWindow(hParent, TRUE);
// SetForegroundWindow(hParent);
SetActiveWindow(hParent);
}
//CreateChildWindow函数的调用处
void OnCommand(HWND hWnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch (id)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case ID_CREATE_MODAL:
{
//创建模态窗口的第一种做法
CreateChildWindow(_T("模态窗口"),_T("ModalChildWin"),true);
break;
}
case ID_CREATE_MODELESS:
{
//创建非模态窗口
CreateChildWindow(_T("非模态窗口"), _T("ModelessChildWin"),false);
break;
}
case ID_DISPLAY_MODAL:
{
DisplayModalWindow(hWnd);
break;
}
default:
return;
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)