Microsoft Documentation
The TrackPopupMenu function displays a shortcut menu at the specified location and enables the user to select an item from the menu.
BOOL TrackPopupMenu(
[in] HMENU hMenu,
[in] UINT uFlags,
[in] int x,
[in] int y,
[in] int nReserved,
[in] const HWND hWnd,
[in, out, optional] RECT *prcExclude
);
Parameter | Description |
---|---|
hMenu |
Handle to the menu to be displayed. This handle can be obtained by calling the CreatePopupMenu function. |
uFlags |
Flags that control the display of the menu. This parameter can be a combination of the following values:
|
x |
The horizontal screen coordinate for the display of the menu. |
y |
The vertical screen coordinate for the display of the menu. |
nReserved |
Reserved; must be zero. |
hWnd |
Handle to the window that owns the shortcut menu. This parameter is required for TPM_RETURNCMD . |
prcExclude |
Pointer to a RECT structure that specifies the screen region that the menu should not overlap. This parameter is used only if TPM_EXCLUDESIZE is specified in uFlags . |
If the function succeeds, it returns the identifier of the menu item selected by the user. If the function fails (e.g., because the menu is invalid or the user cancels the menu without selecting an item), it returns zero.
The TrackPopupMenu function is a convenience function that simplifies the process of tracking shortcut menus.
When tracking a menu, the system sends the WM_COMMAND or WM_SYSCOMMAND message to the owner window when the user selects an item.
hWnd
parameter is valid and points to a visible window if you intend to receive menu command notifications.
// Assuming hWnd is a valid window handle and hMenu is a valid HMENU
// Display the menu at the current mouse cursor position
POINT pt;
GetCursorPos(&pt);
UINT uFlags = TPM_LEFTALIGN | TPM_TOPALIGN | TPM_VERPOSANIMATION | TPM_WORKAREA;
int commandId = TrackPopupMenu(hMenu, uFlags, pt.x, pt.y, 0, hWnd, NULL);
if (commandId == 0) {
// Menu tracking failed or user canceled
} else {
// Process the commandId
// For example:
// switch(commandId) {
// case IDM_OPTION1: ... break;
// case IDM_OPTION2: ... break;
// }
}