7.2 Defining a Menu

The first step in using a menu is to define it in your application's resource-definition (.RC) file by using a MENU statement. A MENU statement consists of the menu name, the MENU keyword, and a pair of BEGIN and END keywords that enclose one or more of the following menu-definition statements:

The MENUITEM statement defines a menu item by name, appearance (text or bitmap), and identifier.

The POPUP statement defines a pop-up menu, which defines further menu items by name, appearance, and identifier.

For example, the following MENU statement defines a menu named SampleMenu:

SampleMenu MENU
    BEGIN
        MENUITEM "Exit!", IDM_EXIT
        MENUITEM "Recalculate!", IDM_RECALC
        POPUP "Options"
        BEGIN
             MENUITEM "Scylla", IDM_SCYLLA
             MENUITEM "Charybdis", IDM_CHARYBDIS
       END
   END

In this example, the first line indicates the beginning of a menu definition and names the menu SampleMenu.

The first MENUITEM statement defines the first item on the menu. The text Exit! will appear as the leftmost item on the menu bar. When the user chooses the Exit! command, Windows sends the application a WM_COMMAND message whose wParam parameter specifies the menu identifier IDM_EXIT. The second MENUITEM statement similarly defines the Recalculate! item.

The POPUP statement defines a pop-up menu named Options that will appear on the menu bar. When the user selects Options from the menu bar, a menu appears in which the user can choose between the Scylla and Charybdis commands.

Within the POPUP statement are two definitions for the Scylla and Charybdis menu items, each with its own text and menu identifier.

When the user chooses the Exit!, Recalculate!, Scylla, or Charybdis command, Windows notifies the application of the user's choice by passing it that item's menu identifier. Note that Windows does not notify the application when the user selects the Options pop-up menu; instead, Windows simply displays that menu.

For more information about the MENU, POPUP, and MENUITEM resource statements, see the Microsoft Windows Programmer's Reference, Volume 4.