Enabling and Disabling Menu Items

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

Setting the Initial State of a Menu Item

In the resource script 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 command is initially grayed:

MENUITEM “Print”, IDM_PRINT, GRAYED

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

Disabling a Menu Item

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

EnableMenuItem (hMenu, IDM_SAVE, MF_DISABLED);

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

Disabling and Graying a Menu Item

So that the user can tell that a menu item is not currently available, it's a good idea to disable a menu item by “graying” it rather than simply disabling it. Graying a menu item disables the item and redisplays the item text in dimmed letters.

To disable and gray a menu item, specify the value MF_GRAYED when you call EnableMenuItem. For example:

EnableMenuItem (hMenu, IDM_PRINT, MF_GRAYED);

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

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 command identified by ID_EXIT:

EnableMenuItem (hMenu, ID_EXIT, MF_ENABLED);