switch(message)
{
case WM_COMMAND:
hMenu = GetMenu(hwnd);
switch(LOWORD(wparam))
{
case IDM_FILE_NEW:
case IDM_FILE_OPEN:
case IDM_FILE_SAVE:
case IDM_FILE_SAVE_AS:
MessageBeep(0);
break;
case IDM_APP_EXIT:
SendMessage(hwnd, WM_CLOSE, 0, 0);
break;
case IDM_EDIT_UNDO:
case IDM_EDIT_CUT:
case IDM_EDIT_COPY:
case IDM_EDIT_PASTE:
case IDM_EDIT_CLEAR:
MessageBeep(0);
break;
case IDM_BKGND_WHITE: // Note: Logic below
case IDM_BKGND_LTGRAY: // assumes that IDM_WHITE
case IDM_BKGND_GRAY: // through IDM_BLACK are
case IDM_BKGND_DKGRAY: // consecutive numbers in
case IDM_BKGND_BLACK: // the order shown here.
CheckMenuItem (hMenu, iSelection, MF_UNCHECKED);
iSelection = LOWORD (wparam);
CheckMenuItem (hMenu, iSelection, MF_CHECKED);
SetClassLong(hwnd, GCL_HBRBACKGROUND,
(LONG)GetStockObject(idColor[LOWORD(wparam) - IDM_BKGND_WHITE]));
InvalidateRect (hwnd, NULL, TRUE);
break;
case IDM_TIMER_START:
if (SetTimer(hwnd, ID_TIMER, 100, NULL))
{
EnableMenuItem(hMenu, IDM_TIMER_START, MF_GRAYED);
EnableMenuItem(hMenu, IDM_TIMER_STOP, MF_ENABLED);
}
break;
case IDM_TIMER_STOP:
KillTimer (hwnd, ID_TIMER);
EnableMenuItem (hMenu, IDM_TIMER_START, MF_ENABLED);
EnableMenuItem (hMenu, IDM_TIMER_STOP, MF_GRAYED);
break;
case IDM_APP_HELP:
MessageBox (hwnd, TEXT("Help not yet implemented!"),
szAppName, MB_ICONEXCLAMATION|MB_OK);
break;
case IDM_APP_ABOUT:
MessageBox (hwnd, TEXT("Menu Demonstration Program/n")
TEXT("(c) Charles Petzold, 1998"),
szAppName, MB_ICONINFORMATION|MB_OK);
break;
}
return 0;
case WM_TIMER:
MessageBeep(0);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hpreinstance, PSTR cmdline, int showcmd)
{
MSG msg;
WNDCLASS wndclass;
HWND hwnd;
hInst = hinstance;
if( (hMenu = LoadMenu(hinstance, MAKEINTRESOURCE(MENUDEMO))) == NULL )
{
MessageBox(0, TEXT("LoadMenu return NULL"), szAppName, MB_ICONERROR);
}
// hMenu = LoadMenu(hinstance, MAKEINTRESOURCE(MENUDEMO));
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(0, IDC_ARROW);
wndclass.hIcon = LoadIcon(0, IDI_APPLICATION);
wndclass.hInstance = hinstance;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = "MENUDEMO";
wndclass.style = CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(0, TEXT("sth wrong~~"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName, Name, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
0, hMenu,
hinstance, 0);
ShowWindow(hwnd, showcmd);
UpdateWindow(hwnd);
while(GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
《PROGRAMMING WINDOWS》例子, 谨记!
//感谢红色那个群的大大。。。