资源描述表中对话框定义
模态对话框
非模态对话框
MessageBox
(本章节中例子都是用 VS2005 编译调试的)
格式:
说明:
步骤:
流程图如下图所示:
回调函数设计模型:
//设计对话框回调函数 //回调函数模型 BOOL CALLBACK DialogProc( HWND hwndDlg, // handle to dialog box该对话框句柄 UINT uMsg, // message WPARAM wParam,// first message parameter LPARAM lParam // second message parameter ) { switch(uMsg) { case WM_INITDIALOG: ……; break; case WM_COMMAND switch(LOWORD(wParam)) { case ……: …… ; break; case ……: …… ; break; …… ; } break; } Return 0; }
与窗口的区别:
窗口(Windows) 对话框(Dialog)
函数返回值 返回LRESULT值 返回BOOL
消息处理 不处理WM_CREAT,WM_DESTORY,WM_PAINT
不处理消息的处理 调用DefWindowProc处理程序不处理的消息 对话框直接返回FALSE(return 0)
对话框处理的主要消息:
代码样例:
对话框 .rc 资源:
IDD_DIALOG1 DIALOGEX 0, 0, 187, 96 STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "对话框" FONT 10, "System", 0, 0, 0x0 BEGIN DEFPUSHBUTTON "确定",IDOK,77,75,50,14 PUSHBUTTON "取消",IDCANCEL,130,75,50,14 EDITTEXT IDC_EDIT1,7,22,173,30,ES_AUTOHSCROLL END
创建对话框:
//创建对话框 DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,dialogFunc); 回调函数设计: switch(msg) { //初始对话框 case WM_INITDIALOG: break; //控件处理 case WM_COMMAND: switch(LOWORD(wParam)) { //按下确定按钮处理 case IDOK: EndDialog(hwndDlg,1); return 0; break; //按下取消按钮处理 case IDCANCEL: EndDialog(hwndDlg,1); return 0; break; } break; //关闭按钮处理 case WM_CLOSE: EndDialog(hwndDlg,1); }
程序源码:
#include<windows.h> #include"resource.h" BOOL CALLBACK dialogFunc( HWND hwndDlg, // handle to dialog box UINT msg, // message WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // pointer to command line int nCmdShow // show state of window ) { DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,dialogFunc); return 0; } BOOL CALLBACK dialogFunc( HWND hwndDlg, // handle to dialog box UINT msg, // message WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch(msg) { //初始对话框 case WM_INITDIALOG: break; //控件处理 case WM_COMMAND: switch(LOWORD(wParam)) { //按下确定按钮处理 case IDOK: EndDialog(hwndDlg,1); return 0; break; //按下取消按钮处理 case IDCANCEL: EndDialog(hwndDlg,1); return 0; break; } break; //关闭按钮处理 case WM_CLOSE: EndDialog(hwndDlg,1); } return 0; }
运行结果:
步骤:
流程图如下:
回调函数设计与模态回调函数设计类似
模态对话框和非模态对话框的区别:
代码示例:
对话框 .rc 资源:
IDD_DIALOG1 DIALOGEX 0, 0, 187, 96 STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "对话框" FONT 10, "System", 0, 0, 0x0 BEGIN DEFPUSHBUTTON "确定",IDOK,77,75,50,14 PUSHBUTTON "取消",IDCANCEL,130,75,50,14 EDITTEXT IDC_EDIT1,7,22,173,30,ES_AUTOHSCROLL END
创建对话框:
hdlg = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,(DLGPROC)dialogFunc);
显示更新对话框:
//显示对话框 ShowWindow(hdlg,nCmdShow); //更新对话框 UpdateWindow(hdlg);
更改父窗口消息循环:
while(GetMessage(&msg,NULL,0,0)) { //判断是否是为对话框消息 if(!IsDialogMessage(hdlg,&msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }
回调函数设计(同模态对话框一样)
程序源码:
#include<windows.h> #include"resource.h" LRESULT CALLBACK textprom( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); BOOL CALLBACK dialogFunc( HWND hwndDlg, // handle to dialog box UINT msg, // message WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // pointer to command line int nCmdShow // show state of window ) { WNDCLASS wndclass; MSG msg; HWND hwnd,hdlg; wndclass.cbClsExtra=0; wndclass.cbWndExtra=0; wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor=LoadCursor(NULL,IDC_ARROW); wndclass.hIcon=LoadIcon(NULL,IDI_ERROR); wndclass.hInstance=hInstance; wndclass.lpfnWndProc=textprom; wndclass.lpszClassName="text"; wndclass.lpszMenuName=NULL; wndclass.style=CS_HREDRAW | CS_VREDRAW; if(!RegisterClass(&wndclass)) { MessageBox(NULL,"create windows error!","error",MB_OK | MB_ICONSTOP); } hwnd = CreateWindow("text","hellow world",WS_DLGFRAME | WS_MINIMIZEBOX | WS_SYSMENU,0,0, 500,300,NULL,NULL,hInstance,NULL); ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); //创建对话框 hdlg = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,(DLGPROC)dialogFunc); //显示更新对话框 ShowWindow(hdlg,nCmdShow); UpdateWindow(hdlg); while(GetMessage(&msg,NULL,0,0)) { if(!IsDialogMessage(hdlg,&msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; } LRESULT CALLBACK textprom( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch(uMsg) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } } BOOL CALLBACK dialogFunc( HWND hwndDlg, // handle to dialog box UINT msg, // message WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch(msg) { case WM_INITDIALOG: break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDOK: DestroyWindow(hwndDlg); return 0; case IDCANCEL: DestroyWindow(hwndDlg); return 0; } break; case WM_CLOSE: DestroyWindow(hwndDlg); } return 0; }
运行结果:
函数说明: Windows消息对话框是模态对话框的一种特殊形式,应用程序可以通过调用函数MessageBox来快捷生成一些简单又常用的Windows消息框.
函数原型:
int MessageBox(
HWND hWnd, // handle of owner window
LPCTSTR lpText, // address of text in message box
LPCTSTR lpCaption, // address of title of message box
UINT uType // style of message box
);
uType说明:
指定一组位标志,来确定对话框的内容和行为.
Flag | Meaning |
MB_ABORTRETRYIGNORE | 三个按钮: 终止, 重试, 忽略. |
MB_OK | 一个按钮: 确定. |
MB_OKCANCEL | 两个按钮: 确定,取消. |
MB_RETRYCANCEL | 两个按钮: 重试,取消. |
MB_YESNO | 两个按钮: 是,否. |
MB_YESNOCANCEL | 三个按钮: 是, 否, 取消. |
Flag | Meaning |
MB_ICONEXCLAMATION MB_ICONWARNING |
|
MB_ICONINFORMATION MB_ICONASTERISK |
|
MB_ICONQUESTION | |
MB_ICONSTOP MB_ICONERROR MB_ICONHAND |
Flag | Meaning |
MB_DEFBUTTON1 | 缺省按钮为第一个按钮 |
MB_DEFBUTTON2 | 缺省按钮为第二个按钮 |
MB_DEFBUTTON3 | 缺省按钮为第三个按钮 |
MB_DEFBUTTON4 | 缺省按钮为第四个按钮 |
Flag | Meaning |
MB_APPLMODAL | 引用程序对话框(创建的是非模态的对话框) |
MB_SYSTEMMODAL | 系统对话框(对话框会保持于窗体 z_order 的顶层) |
MB_TASKMODAL | 任务对话框(创建的是模态的对话框) |
代码样例:
创建具有是否按钮和问好图标的消息框,并且消息框为模态对话框:
MessageBox(NULL,"hello","test",MB_YESNO|MB_ICONQUESTION|MB_TASKMODAL);
程序结果: