Checking and Unchecking Menu Items

You can display a checkmark next to an item to indicate that the user has selected it. Typically, you check a menu item when it is part of a group of items that are mutually exclusive. The checkmark indicates the user's latest choice. For example, if a group consists of the items Left, Right, and Center, you might check the Left item to indicate that the user chose that item most recently.

Setting an Initial Checkmark

In the resource script file, you can specify whether a menu item is initially checked. To do so, use the CHECKED option in the MENUITEM statement. For example, the following MENUITEM statement specifies that the Left command is initially checked:

MENUITEM “Left”, IDM_LEFT, CHECKED

Checking a Menu Item

The information in the resource script file applies only to the initial state of the menu. You can check or remove a checkmark from a menu item later, using the CheckMenuItem function in your C-language source file. CheckMenuItem checks or removes a checkmark from a specified menu item.

The following example places a checkmark next to the item whose menu ID is IDM_LEFT:

CheckMenuItem (hMenu, IDM_LEFT, MF_CHECKED);

Removing a Menu-Item Checkmark

To remove a checkmark from a menu item, you call the CheckMenuItem function and specify the value MF_UNCHECKED. The following example removes the check (if any) from the item whose menu ID is IDM_RIGHT:

CheckMenuItem (hMenu, IDM_RIGHT, MF_UNCHECKED);

If you change menu items in the menu bar, you need to call the DrawMenuBar function to display the changes.