Menu Resource Example
This example demonstrates a typical .rc file that defines a main menu with File, Edit, and Help items. It also shows how to associate command IDs with menu commands.
#include <windows.h>
#define IDM_FILE_NEW 40001
#define IDM_FILE_OPEN 40002
#define IDM_FILE_SAVE 40003
#define IDM_FILE_EXIT 40004
#define IDM_EDIT_UNDO 40101
#define IDM_EDIT_CUT 40102
#define IDM_EDIT_COPY 40103
#define IDM_EDIT_PASTE 40104
#define IDM_HELP_ABOUT 40201
IDR_MAINMENU MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "&New", IDM_FILE_NEW
MENUITEM "&Open...", IDM_FILE_OPEN
MENUITEM "&Save", IDM_FILE_SAVE
MENUITEM SEPARATOR
MENUITEM "E&xit", IDM_FILE_EXIT
END
POPUP "&Edit"
BEGIN
MENUITEM "&Undo", IDM_EDIT_UNDO, GRAYED
MENUITEM SEPARATOR
MENUITEM "Cu&t", IDM_EDIT_CUT
MENUITEM "&Copy", IDM_EDIT_COPY
MENUITEM "&Paste", IDM_EDIT_PASTE
END
POPUP "&Help"
BEGIN
MENUITEM "&About...", IDM_HELP_ABOUT
END
END
Compile the resource file with rc.exe and link it into your application. The defined command IDs can be processed in your window procedure:
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_FILE_NEW: // handle New
// ...
break;
case IDM_FILE_OPEN: // handle Open
// ...
break;
case IDM_FILE_EXIT:
PostQuitMessage(0);
break;
// other cases...
}
break;
// other messages...
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}