You can add a new menu item to the end of an existing menu, or insert one after a particular menu item.
To append an item to the end of an existing menu, you use the AppendMenu function. This function adds a new item to the end of the specified menu, and lets you specify whether the new item is checked, enabled, grayed, and so on.
The following example appends the item Raspberries to the end of the Fruit menu. The example disables and grays the new item if raspberries are not currently in season.
if(!RasberriesInSeason)
AppendMenu (hFruitMenu,
MF_GRAYED,
IDM_RASPBERRIES,
“Raspberries”);
else
AppendMenu (hFruitMenu,
MF_ENABLED,
IDM_RASPBERRIES,
“Raspberries”);
To insert an item in an existing menu, you use the InsertMenu function. This function inserts the specified item at the specified position and moves subsequent items down to accommodate the new item. Like AppendMenu, InsertMenu lets you specify the state of the new item when you insert it.
The following example inserts the item Kumquats before the existing item Melons. The example disables and grays the new item.
InsertMenu (hFruitMenu,
IDM_MELONS,
MF_BYCOMMAND | MF_GRAYED,
IDM_KUMQUATS,
“Kumquats”);
You can also insert items by numerical position rather than before a specific item. The following example inserts the item Bananas so that it becomes 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”);