Overview
The MENU
resource defines a menu bar or popup menu that can be loaded with LoadMenu
, LoadMenuIndirect
, or CreateMenu
in a Win32 application.
A menu consists of one or more MENUITEM
entries that describe individual items, separators, sub‑menus, and their associated command identifiers.
Syntax
MENU [MENU_ID] {
MENUITEM [flags], "caption", [id_or_submenu] [, helpid]
POPUP "caption" {
MENUITEM [...]
SEPARATOR
POPUP [...]
}
SEPARATOR
}
Key symbols:
MENU_ID
– Optional integer identifier for the menu resource.flags
– One or more ofMFS_DISABLED
,MFS_CHECKED
,MFS_DEFAULT
, etc.id_or_submenu
– Command identifier for the item or aPOPUP
resource name.helpid
– Optional help identifier used byWM_HELP
.
MENUITEMINFO fields (Win32 API)
Field | Description |
---|---|
cbSize | Size of the structure, set to sizeof(MENUITEMINFO) . |
fMask | Specifies which members contain valid data (e.g., MII_STRING , MII_ID ). |
fType | Item type (MFT_STRING , MFT_SEPARATOR , MFT_BITMAP ). |
fState | Item state (MFS_CHECKED , MFS_DISABLED ). |
wID | Command identifier. |
hbmpChecked | Bitmap for a checked state. |
dwTypeData | Pointer to a string for the caption. |
Sample Resource Script
#include "winres.h"
#define IDM_FILE_NEW 40001
#define IDM_FILE_OPEN 40002
#define IDM_FILE_EXIT 40003
#define IDM_EDIT_UNDO 40004
#define IDM_HELP_ABOUT 40005
MENU RESOURCE_ID, L"MAINMENU", 0, 0
BEGIN
POPUP "&File"
BEGIN
MENUITEM "&New", IDM_FILE_NEW
MENUITEM "&Open...", IDM_FILE_OPEN
SEPARATOR
MENUITEM "E&xit", IDM_FILE_EXIT, GRAYED
END
POPUP "&Edit"
BEGIN
MENUITEM "&Undo", IDM_EDIT_UNDO, CHECKED
POPUP "&Advanced"
BEGIN
MENUITEM "Option &A", 40010
MENUITEM "Option &B", 40011, GRAYED
END
END
POPUP "&Help"
BEGIN
MENUITEM "&About", IDM_HELP_ABOUT
END
END
Loading a MENU Resource in Code
#include <windows.h>
HMENU hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(RESOURCE_ID));
if (hMenu) {
SetMenu(hWnd, hMenu);
} else {
MessageBox(hWnd, L"Failed to load menu.", L"Error", MB_ICONERROR);
}
The LoadMenu
function retrieves the compiled menu resource, and SetMenu
attaches it to a window.