Menu Templates

Menu templates are a fundamental concept in Windows application development, allowing developers to define the structure and content of application menus in a declarative way. These templates are typically created using resource editors and are compiled into the application's executable or a separate resource file.

Resource Editor Usage

Most integrated development environments (IDEs) for Windows development, such as Visual Studio, provide a visual menu editor. This editor allows you to:

Menu Template Structure

Menu templates are stored in a resource script file (typically with a .rc extension) and are compiled into a binary format that the Windows operating system can load at runtime. A simplified representation of a menu template in a resource script might look like this:


IDR_MYMENU MENU
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "&New", ID_FILE_NEW
        MENUITEM "&Open", ID_FILE_OPEN
        MENUITEM SEPARATOR
        MENUITEM "&Save", ID_FILE_SAVE
        MENUITEM "Save &As...", ID_FILE_SAVE_AS
        MENUITEM SEPARATOR
        MENUITEM "E&xit", ID_FILE_EXIT
    END

    POPUP "&Edit"
    BEGIN
        MENUITEM "&Undo\tCtrl+Z", ID_EDIT_UNDO
        MENUITEM "&Redo\tCtrl+Y", ID_EDIT_REDO
        MENUITEM SEPARATOR
        MENUITEM "Cu&t\tCtrl+X", ID_EDIT_CUT
        MENUITEM "&Copy\tCtrl+C", ID_EDIT_COPY
        MENUITEM "&Paste\tCtrl+V", ID_EDIT_PASTE
    END

    POPUP "&Help"
    BEGIN
        MENUITEM "&About...", ID_HELP_ABOUT
    END
END

            

Key Elements in Menu Templates

Loading Menus

In your application code, you typically load the menu template using the LoadMenu API function, passing the resource identifier (e.g., IDR_MYMENU). This function returns a handle to the menu resource, which can then be attached to a window using SetMenu.

Note on Command IDs

Command IDs are typically defined as constants in a header file (e.g., resource.h) to ensure consistency between the resource script and your application code. These IDs are sent as wParam in WM_COMMAND messages when a menu item is selected.

Advanced Menu Features

Menu templates can also support more advanced features:

See Also