头文件x.h
#define ID_SAVE 12
#define ID_OPEN 13
#define ID_EXIT 14
#define ID_ABOUT 15
资源文件 x.rc
#include <windows.h>
#include "5_4.h"
Menu MENU DISCARDABLE
{
POPUP "文件(&F)"
{
MENUITEM "新建(&N)\t Ctrl+N", IDM_NEW
MENUITEM "打开(&O)\t Ctrl+O", IDM_OPEN
MENUITEM SEPARATOR
MENUITEM "保存(&S)\t Ctrl+S", IDM_SAVE
MENUITEM SEPARATOR
MENUITEM "退出(&X)", IDM_EXIT
}
POPUP "帮助(&H)"
{
MENUITEM "关于(&A)...", IDM_ABOUT
}
}
Menu ACCELERATORS
{
"^N", IDM_NEW
"^O", IDM_OPEN
"^S", IDM_SAVE
}
About DIALOG 50, 50, 100, 50
STYLE DS_MODALFRAME|WS_POPUP | WS_CAPTION | WS_SYSMENU|WS_VISIBLE//非模态对话框
//的风格中应包含WS_VISIBLE
CAPTION "关于"
FONT 12, "楷体"
{
CTEXT "Windows对话框示例",-1,13,10,80,10
DEFPUSHBUTTON "确定",IDOK,35,30,30,12
}
#include <windows.h>
#include "x.h"
HWND hdlg; //对话框句柄
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd;
MSG Msg;
HACCEL hAccel;
WNDCLASS wndclass;
char lpszMenuName[] = "Menu";
char lpszClassName[] = "非模态对话框";
char lpszTitle[] = "非模态对话框演示程序";
wndclass.style = 0;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = lpszMenuName;
wndclass.lpszClassName = lpszClassName;
if (!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
}
hwnd = CreateWindow(lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
hInst = hInstance;
hAccel = LoadAccelerators(hInst, lpszMenuName);
while (GetMessage(&Msg, NULL, 0, 0))
{
//截获非模态对话框消息并发往处理函数
if (!IsDialogMessage(hdlg, &Msg))
{
if (!TranslateAccelerator(hwnd, hAccel, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_OPEN:
MessageBox(hwnd, "文件打开成功!", "文件打开", MB_OK);
break;
case ID_SAVE:
MessageBox(hwnd, "文件保存成功!", "文件保存", MB_OK);
break;
case ID_EXIT:
SendMessage(hwnd, WM_DESTROY, 0, 0);
break;
case ID_ABOUT:
//调用函数CreateDialog创建非模态对话框
hdlg = CreateDialog(hInst, "About", hwnd, (DLGPROC)DlgProc);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
//对话框窗口处理函数
BOOL CALLBACK DlgProc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG: //初始化消息框
return 1;
case WM_COMMAND: //处理消息框消息
switch (LOWORD(wParam))
{
case IDOK:
DestroyWindow(hdlg);
}
break;
case WM_CLOSE:
DestroyWindow(hdlg);
break;
}
return 0;
}