7.5.1 Enabling and Disabling Menu Items

Usually, a menu item is enabled; its text appears normal, and the user can choose it as a command. A disabled item appears normal but does not respond to mouse clicks or keyboard selection. An unavailable item has grayed (sometimes called “dimmed”) text and does not respond to mouse clicks or keyboard selection. Typically, you disable a menu item, or make it unavailable, when the action it represents is not appropriate. For example, you might make the Print command in the File menu unavailable when the system does not have a printer installed.

7.5.1.1 Setting the Initial State of a Menu Item

In the resource-definition file, you can specify whether a menu item is initially disabled or grayed. To do so, use the INACTIVE or GRAYED options with the MENUITEM statement. For example, the following statement specifies that the Print menu item is initially grayed:

MENUITEM "Print", IDM_PRINT, GRAYED

The information in the resource-definition file applies only to the initial state of the menu. You can change the command's state later, using the EnableMenuItem function in your C-language source file. EnableMenuItem enables or disables a menu item or makes it unavailable.

7.5.1.2 Disabling a Menu Item

A disabled menu item appears normal but does not respond to mouse clicks or keyboard selection. A disabled item is commonly used as a title for related menu options. The following example disables a menu item:

EnableMenuItem(hMenu, IDM_SAVE, MF_DISABLED);

This example disables a menu item on the menu represented by the menu handle hMenu. The menu identifier of the item is IDM_SAVE. By specifying the value MF_DISABLED, you direct Windows to disable the specified item.

7.5.1.3 Disabling a Menu Item and Making It Unavailable

So that the user can tell that a command is not currently available, you may want your application to make it unavailable rather than simply disabling it. Making a menu item unavailable disables it and redisplays its text in grayed letters. To disable a menu item and make it unavailable, specify the value MF_GRAYED when you call EnableMenuItem, as in the following example:

EnableMenuItem(hMenu, IDM_PRINT, MF_GRAYED);

This example disables an item on the menu represented by the menu handle hMenu. The menu identifier of the item is IDM_PRINT. By specifying the value MF_GRAYED, you tell Windows to disable the specified item and redisplay its text in grayed letters.

7.5.1.4 Enabling a Menu Item

You can enable a disabled menu item by calling EnableMenuItem and specifying the MF_ENABLED value. The following example enables the item identified by ID_EXIT:

EnableMenuItem(hMenu, ID_EXIT, MF_ENABLED);