/* menu1.cpp Windows program with a complex menu */
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage,
WPARAM wParam, LPARAM lParam);
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine,
int nCmdShow)
{
HWND hWnd ; /* the window's "handle" */
MSG msg ; /* a message structure */
WNDCLASS wndclass ; /* window class structure */
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = NULL ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = "BIGMENU" ;
wndclass.lpszClassName = "MyClass" ;
/* register the window class */
if (!RegisterClass (&wndclass))
return 0 ;
hWnd = CreateWindow ("MyClass", "Menu Example 1", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hWnd, nCmdShow) ; /* display the window */
UpdateWindow (hWnd) ;
while (GetMessage (&msg, NULL, 0, 0)) /* message loop */
{
TranslateMessage (&msg) ; /* translate keyboard messages */
DispatchMessage (&msg) ; /* send message to WndProc() */
}
return ((int)msg.wParam) ;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage,
WPARAM wParam, LPARAM lParam)
{
static BOOL bCheckOnOff = TRUE ; /* checked status of Sel 2 */
HMENU hMenu ;
switch (wMessage) /* process windows messages */
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_SEL1: /* menu items */
MessageBox (hWnd, "Selection 1 enables Selection 3",
"Surprise!", MB_OK) ;
hMenu = GetMenu (hWnd) ; /* get menu handle */
EnableMenuItem (hMenu, IDM_SEL3,
MF_BYCOMMAND | MF_ENABLED ) ;
break ;
case IDM_SEL2: /* this one can be checked */
bCheckOnOff = (bCheckOnOff ? FALSE : TRUE) ;
hMenu = GetMenu (hWnd) ; /* get menu handle */
if (bCheckOnOff)
CheckMenuItem (hMenu, IDM_SEL2,
MF_BYCOMMAND | MF_CHECKED) ;
else
CheckMenuItem (hMenu, IDM_SEL2,
MF_BYCOMMAND | MF_UNCHECKED) ;
break ;
case IDM_SEL3: /* initially grayed */
MessageBox (hWnd, "Selection 3 now enabled.", "", MB_OK) ;
break ;
case IDM_RIGHT:
MessageBox (hWnd, "Right side disables Selection 3.",
"Surprise!", MB_OK) ;
hMenu = GetMenu (hWnd) ; /* get menu handle */
EnableMenuItem (hMenu, IDM_SEL3,
MF_BYCOMMAND | MF_GRAYED ) ;
break ;
case IDM_LEFT:
MessageBox (hWnd, "Left side just beeps.", "", MB_OK) ;
MessageBeep (0) ;
break ;
case IDM_HELP:
MessageBox (hWnd, "Try selecting any menu item.",
"Not much help", MB_OK) ;
break ;
case IDM_QUIT: /* Quit menu item */
DestroyWindow (hWnd) ; /* destroy window, */
break ; /* terminating application */
default: /* otherwise, let Windows process it */
return (DefWindowProc (hWnd, wMessage, wParam, lParam)) ;
}
break ;
case WM_DESTROY: /* stop application */
PostQuitMessage (0) ;
break ;
default: /* default windows message processing */
return (DefWindowProc (hWnd, wMessage, wParam, lParam)) ;
}
return (0) ;
}