模态窗口的创建由DialogBox()函数。
首先定义几个id:
#define ID_FILE_EXIT 900 #define ID_STUFF_GO 9002 #define ID_HELP_ABOUT 9003 #define IDD_ABOUT 101
对话框的资源文件如下:
///////////////////////////////////////////////////////////////////////////// // // Dialog // IDD_ABOUT DIALOGEX 0, 0, 316, 183 STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "My About Box" FONT 8, "MS Sans Serif" BEGIN DEFPUSHBUTTON "&OK",IDOK,174,18,50,14 PUSHBUTTON "&Cancel",IDCANCEL,174,35,50,14 GROUPBOX "About this program...",IDC_STATIC,7,7,225,52 CTEXT "An example program showing how to use Dialog Boxes\r\n\r\nby wql", IDC_STATIC,16,18,144,33 END /////////////////////////////////////////////////////////////////////////////
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: switch(LOWORD(wParam)) { case IDOK: EndDialog(hwnd, IDOK); break; case IDCANCEL: EndDialog(hwnd, IDCANCEL); break; } break; default: return FALSE; } return TRUE; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: { HMENU hMenu, hSubMenu; HICON hIcon, hIconSm; hMenu = CreateMenu(); hSubMenu = CreatePopupMenu(); AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit"); AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File"); hSubMenu = CreatePopupMenu(); AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&Go"); AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff"); hSubMenu = CreatePopupMenu(); AppendMenu(hSubMenu,MF_STRING , ID_HELP_ABOUT, "&About"); AppendMenu(hMenu,MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help"); SetMenu(hwnd, hMenu); hIcon = (HICON)LoadImage(NULL, "icon1.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE); if(hIcon) SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon); else MessageBox(hwnd, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR); hIconSm = (HICON)LoadImage(NULL, "icon1.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE); if(hIconSm) SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm); else MessageBox(hwnd, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR); } break; case WM_COMMAND: switch(LOWORD(wParam)) { case ID_HELP_ABOUT: { int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc); //创建模态窗口 if(ret == IDOK){ MessageBox(hwnd, "Dialog exited with IDOK.", "Notice", MB_OK | MB_ICONINFORMATION); } else if(ret == IDCANCEL){ MessageBox(hwnd, "Dialog exited with IDCANCEL.", "Notice", MB_OK | MB_ICONINFORMATION); } else if(ret == -1){ MessageBox(hwnd, "Dialog failed!", "Error", MB_OK | MB_ICONINFORMATION); } } break; case ID_FILE_EXIT: PostMessage(hwnd,WM_CLOSE,NULL,NULL); break; case ID_STUFF_GO: MessageBox(hwnd,"you click the GO","Menu MessageBox",MB_OK | MB_ICONEXCLAMATION); break; } break; case WM_LBUTTONDOWN: { char szFileName[MAX_PATH]; HINSTANCE hInstance = GetModuleHandle(NULL); GetModuleFileName(hInstance, szFileName, MAX_PATH); MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION); } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; }
这就是我用来创建我的关于对话框的代码,你应该看出来这应该移到你的WM_COMMAND消息处理的代码中去,如果你对这点还不清楚,你可能需要复习关于菜单的那篇文章.ID_HELP_ABOUT是我的Help->About菜单项的标识.
因为我们要我们的主窗口的菜单来创建这个对话话,我们显然需要把这些代码放到我们主窗口的WndProc()中去,而不是对话框的过程中.
现在我存储了调用DialogBox()的返回值,这样你就可以观察你点击两个按钮的效果,在对话框中按Esc,Enter等等...这样做也展示了如何通过对话框的返回值来判断是否调用成功,和用戶用选择,或是你想从你的对话框过程中想返回给调用者的任何消息.
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);这是唯一重要的地方,你可以把这段代码放到你想要对话框出现的任何地方.IDD_ABOUT是对话框资源的标识.hwnd是对话框的父窗口的句柄.AboutDlgProc()当然是用来控制对话框的对话过程.