About Menus (Win32 Resource Files)

Overview

In Win32 applications, menus are defined in a resource script (.rc) file using the MENU and MENUITEM statements. The resource compiler translates these definitions into binary menu resources that the system loads at runtime.

Why use menu resources?

Typical structure

// Sample.rc
MENU
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "E&xit",            IDM_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About",          IDM_ABOUT
    END
END

Menu Resource Syntax

The MENU block consists of POPUP and MENUITEM entries. Each entry can include flags to control behavior.

Key symbols

Example with flags

MENU
BEGIN
    POPUP "&View"
    BEGIN
        MENUITEM "&Toolbar",       IDM_VIEW_TOOLBAR, CHECKED
        MENUITEM "Status &Bar",     IDM_VIEW_STATUS, CHECKED
        MENUITEM "-",                0,                SEPARATOR
        MENUITEM "Zoom &In",        IDM_ZOOM_IN,      GRAYED
        MENUITEM "Zoom &Out",       IDM_ZOOM_OUT
    END
END

Loading Menus in Code

Typical usage in a Win32 application involves calling LoadMenu or LoadMenuW and attaching the menu to a window with SetMenu.

#include <windows.h>

HMENU hMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_MAINMENU));
SetMenu(hWnd, hMenu);

// To handle a command:
case WM_COMMAND:
    switch (LOWORD(wParam)) {
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        case IDM_ABOUT:
            DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_ABOUT), hWnd, AboutDlgProc);
            break;
    }
    break;

Related Topics