The Menu Template

You can create a menu in three different ways. The most common (and the easiest) is to define the menu in your resource script in the form of a menu template. This example shows all the different options you can use in this template.

MyMenu MENU [load option] [memory option]

{MENUITEM "&One", 1

POPUP "&Two"

{MENUITEM "&Ten", 10, CHECKED

MENUITEM "&Eleven", 11

MENUITEM SEPARATOR

MENUITEM "T&welve", 12, INACTIVE

MENUITEM "T&hirteen", 13

MENUITEM "&Fourteen", 14, MENUBREAK

MENUITEM "F&ifteen", 15,

MENUITEM "&Sixteen", 16, MENUBARBREAK

POPUP "Se&venteen", 17,

{MENUITEM "&Twenty", 20

MENUITEM "T&wenty-One", 21

MENUITEM "Tw&enty-Two", 22

}MENUITEM "Ei&ghteen", 18, GRAYED

}MENUITEM "Th&ree", 3

MENUITEM "&Four", 4, INACTIVE

MENUITEM "Fi&ve", 5

MENUITEM "Si&x", 6, MENUBREAK

MENUITEM "&Seven", 7,

MENUITEM "&Eight", 8, GRAYED

MENUITEM "\a&Help", 9, HELP

}This particular menu template defines a top-level menu that displays the labels ”One“ through ”Eight“ and ”Help,“ as shown in Figure 9-1 on the following page. Only the second item invokes a popup menu. The popup menu displays the labels ”Ten“ through ”Eighteen,“ as shown in Figure 9-2 on the following page. The little arrow to the right of the ”Seventeen“ option indicates that it invokes yet another popup menu.

MyMenu is the name of the menu. This name performs the same function as the names of icon, cursor, and bitmap resources discussed in Chapter 8. As with other resources, the load option on the MENU statement can be either PRELOAD (in which case Windows loads the resource into memory when the program is executed) or LOADONCALL (in which case Windows loads the resource into memory only when it is needed). The default is LOADONCALL. The memory options are FIXED, MOVEABLE, and DISCARDABLE. The default is MOVEABLE and DISCARDABLE. Discardable menus must also be moveable. Although we'll change menus in some of the programs shown later, don't worry that the menu resource is discardable. Windows makes a copy of the menu for your program to use and change.

The top-level menu is enclosed in left and right brackets. (You can use BEGIN and END statements instead if you wish.) The two types of statements allowed within these brackets are:

MENUITEM "text", wID, options

and:

POPUP "text", options

The text displayed for each menu must be enclosed in double quotation marks. An ampersand (&) causes the character that follows it to be underlined when Windows displays the menu. This is also the character Windows searches for when you select a menu item using the Alt key. If you don't include an ampersand in the text, no underline will appear, and Windows will use instead the first letter of the text for Alt-key searches.

The options on the MENUITEM and POPUP statements that appear in the top-level menu list are as follows:

GRAYED—The menu item is inactive, and it does not generate a WM_COMMAND message. The text is grayed.

INACTIVE—The menu item is inactive, and it does not generate a WM_COMMAND message. The text is displayed normally.

MENUBREAK—This item and following items appear on a new line of the menu.

HELP—When used in combination with \a before the text, this item is right-justified.

Options can be combined using the C bitwise OR symbol (|), but GRAYED and INACTIVE cannot be used together. MENUBREAK is uncommon in a top-level menu, because Windows automatically separates a top-level menu into multiple lines if the window is too narrow to fit the entire menu.

Following a POPUP statement in the main menu, the left and right brackets (or the BEGIN and END keywords) block off a list of items in the popup. The following statements are allowed in a popup definition:

MENUITEM "text", wID, options

and:

MENUITEM SEPARATOR

and:

POPUP "text", options

MENUITEM SEPARATOR draws a horizontal line in the popup menu. This line is often used to separate groups of related options.

For items in popup menus, you can use the columnar tab character \t in the text string. Text following the \t is placed in a new column spaced far enough to the right to accommodate the longest text string in the first column of the popup. We'll see how this works when discussing keyboard accelerators toward the end of this chapter. A \a right-justifies the text that follows it. The options for MENUITEM in a popup are as follows:

CHECKED—A check mark appears to the left of the text.

GRAYED—The menu item is inactive and does not generate a WM_COMMAND message. The text is grayed.

INACTIVE—The menu item is inactive and does not generate a WM_COMMAND message. The text is displayed normally.

MENUBREAK—This item and the following items appear in a new column of the menu.

MENUBARBREAK—This item and the following items appear in a new column of the menu. A vertical line separates the columns.

GRAYED and INACTIVE cannot be used together. MENUBREAK and MENUBARBREAK cannot be used together. You should use either MENUBREAK or MENUBARBREAK when the number of items in a popup is too long to be displayed in a single column.

The wID values in the MENUITEM statements are the numbers that Windows sends to the window procedure in menu messages. The wID values should be unique within a menu. Instead of using numbers, you'll probably want to use identifiers defined in a header file. By convention, these identifiers begin with the letters IDM (”ID for a menu“).