7.5.3 Adding Menu Items

You can add new items to the end of existing menus, or insert new items after particular items.

7.5.3.1 Appending an Item to an Existing Menu

To append an item to the end of an existing menu, you use the AppendMenu function. With this function, you can add a new item to the end of the specified menu and specify whether the new item is checked, enabled, grayed, and so on.

The following example appends the menu item Raspberries to the end of the Fruit menu. The example disables the new item and makes it unavailable if raspberries are not currently in season.

AppendMenu(hFruitMenu,
    RaspberriesInSeason ? MF_ENABLED : MF_GRAYED,
    IDM_RASPBERRIES,
    "Raspberries");

7.5.3.2 Inserting an Item in an Existing Menu

To insert an item in an existing menu, you use the InsertMenu function. This function inserts the specified menu item at the specified position and moves subsequent items down to accommodate the new item. Like the AppendMenu function, InsertMenu lets you specify the state of the new menu item when you insert it.

The following example inserts the menu item Kumquats before the existing item Melons. The example disables the new item and makes it unavailable.

InsertMenu(hFruitMenu,
    IDM_MELONS,
    MF_BYCOMMAND | MF_GRAYED,
    IDM_KUMQUATS,
    "Kumquats");

You can also insert menu items by numerical position rather than before a specific item. The following example inserts the item Bananas, making it the third item in the Fruit menu. (The first item has position 0, the second item 1, and so on.)

InsertMenu(hFruitMenu,
    2,
    MF_BYPOSITION | MF_GRAYED,
    IDM_BANANAS,
    "Bananas");