Displaying a Shortcut Menu

The function shown in the following example displays a shortcut menu.

The application includes a menu resource identified by the string "ShortcutExample." The menu bar simply contains a menu name. The application uses the TrackPopupMenu function to display the menu associated with this menu item. (The menu bar itself is not displayed because TrackPopupMenu requires the handle of a menu, submenu, or shortcut menu.)

VOID APIENTRY DisplayContextMenu(HWND hwnd, POINT pt)

{

HMENU hmenu; // top-level menu

HMENU hmenuTrackPopup; // shortcut menu

// Load the menu resource.

if ((hmenu = LoadMenu(hinst, "ShortcutExample")) == NULL)

return;

// TrackPopupMenu cannot display the menu bar so get

// the handle of the first shortcut menu.

hmenuTrackPopup = GetSubMenu(hmenu, 0);

// Display the shortcut menu. Track the right mouse

// button.

TrackPopupMenu(hmenuTrackPopup,

TPM_LEFTALIGN | TPM_RIGHTBUTTON,

pt.x, pt.y, 0, hwnd, NULL);

// Destroy the menu.

DestroyMenu(hmenu);

}