windows_programming_notes.nbk: Home | Index | Next Page: TrackPopupMenuEx | Previous Page: TrackMouseEvent
TrackPopupMenu
The TrackPopupMenu function displays a shortcut menu at the specified location and tracks the selection of items on the menu. The shortcut menu can appear anywhere on the screen.
Syntax
BOOL TrackPopupMenu(
HMENU hMenu,
UINT uFlags,
int x,
int y,
int nReserved,
HWND hWnd,
HWND prcRect
);
Parameters
- hMenu
[in] Handle to the shortcut menu to be displayed. The handle can be obtained by calling CreatePopupMenu to create a new shortcut menu, or by calling GetSubMenu to retrieve a handle to a submenu associated with an existing menu item.
uFlags
[in] Use zero of more of these flags to specify function options.
Use one of the following flags to specify how the function positions the shortcut menu horizontally.
TPM_CENTERALIGN
If this flag is set, the function centers the shortcut menu horizontally relative to the coordinate specified by the x parameter.
TPM_LEFTALIGN
If this flag is set, the function positions the shortcut menu so that its left side is aligned with the coordinate specified by the x parameter.
TPM_RIGHTALIGN
Positions the shortcut menu so that its right side is aligned with the coordinate specified by the x parameter.
Use one of the following flags to specify how the function positions the shortcut menu vertically.
TPM_BOTTOMALIGN
If this flag is set, the function positions the shortcut menu so that its bottom side is aligned with the coordinate specified by the y parameter.
TPM_TOPALIGN
If this flag is set, the function positions the shortcut menu so that its top side is aligned with the coordinate specified by the y parameter.
TPM_VCENTERALIGN
If this flag is set, the function centers the shortcut menu vertically relative to the coordinate specified by the y parameter.
Use the following flags to determine the user selection without having to set up a parent window for the menu.
TPM_NONOTIFY
If this flag is set, the function does not send notification messages when the user clicks on a menu item.
TPM_RETURNCMD
If this flag is set, the function returns the menu item identifier of the user's selection in the return value.
Use one of the following flags to specify which mouse button the shortcut menu tracks.
TPM_LEFTBUTTON
If this flag is set, the user can select menu items with only the left mouse button.
TPM_RIGHTBUTTON
If this flag is set, the user can select menu items with both the left and right mouse buttons.
Windows 98/Me, Windows 2000/XP:Use any reasonable combination of the following flags to modify the animation of a menu. For example, by selecting a horizontal and a vertical flag you can achieve diagonal animation.
TPM_HORNEGANIMATION
Animates the menu from right to left.
TPM_HORPOSANIMATION
Animates the menu from left to right.
TPM_NOANIMATION
Displays menu without animation.
TPM_VERNEGANIMATION
Animates the menu from bottom to top.
TPM_VERPOSANIMATION
Animates the menu from top to bottom.
For any animation to occur, the SystemParametersInfo function must set SPI_SETMENUANIMATION. Also, all the TPM_*ANIMATION flags, except TPM_NOANIMATION, are ignored if menu fade animation is on, See the SPI_GETMENUFADE flag in SystemParametersInfo.
Windows 98/Me, Windows 2000/XP: Use the TPM_RECURSE flag to display a menu when another menu is already displayed. This is intended to support context menus within a menu.
Windows XP: To have text layout from right-to-left, use TPM_LAYOUTRTL. By default, the text layout is left-to-right.
x
[in] Specifies the horizontal location of the shortcut menu, in screen coordinates.
y
[in] Specifies the vertical location of the shortcut menu, in screen coordinates.
nReserved
Reserved; must be zero.
hWnd
[in] Handle to the window that owns the shortcut menu. This window receives all messages from the menu. The window does not receive a WM_COMMAND message from the menu until the function returns. If you specify TPM_NONOTIFY in the uFlags parameter, the function does not send messages to the window identified by hWnd. However, you must still pass a window handle in hWnd. It can be any window handle from your application.
prcRect
Ignored.
Return Value
If you specify TPM_RETURNCMD in the uFlags parameter, the return value is the menu-item identifier of the item that the user selected. If the user cancels the menu without making a selection, or if an error occurs, then the return value is zero.
If you do not specify TPM_RETURNCMD in the uFlags parameter, the return value is nonzero if the function succeeds and zero if it fails. To get extended error information, call GetLastError.
Remarks
Note Call GetSystemMetrics(SM_MENUDROPALIGNMENT) to determine the correct horizontal alignment flag (TPM_LEFTALIGN or TPM_RIGHTALIGN) and/or horizontal animation direction flag (TPM_HORPOSANIMATION or TPM_HORNEGANIMATION) to pass to TrackPopupMenu or TrackPopupMenuEx. This is essential for creating an optimal user experience, especially when developing Microsoft Tablet PC applications.
To specify an area of the screen the menu should not overlap, use the TrackPopupMenuEx function
To display a context menu for a notification icon, the current window must be the foreground window before the application calls TrackPopupMenu or TrackPopupMenuEx. Otherwise, the menu will not disappear when the user clicks outside of the menu or the window that created the menu (if it is visible). However, when the current window is the foreground window, the second time this menu is displayed, it displays and then immediately disappears. To correct this, you must force a task switch to the application that called TrackPopupMenu. This is done by posting a benign message to the window or thread, as shown in the following code sample:
SetForegroundWindow(hDlg);
// Display the menu
TrackPopupMenu( hSubMenu,
TPM_RIGHTBUTTON,
pt.x,
pt.y,
0,
hDlg,
NULL);
PostMessage(hDlg, WM_NULL, 0, 0);
Function Information
- Minimum DLL Version user32.dll
- Header Declared in Winuser.h, include Windows.h
- Import library User32.lib
- Minimum operating systems Windows 95, Windows NT 3.1
- Unicode Implemented as Unicode version.
windows_programming_notes.nbk: Home | Index | Next Page: TrackPopupMenuEx | Previous Page: TrackMouseEvent
Notebook exported on Monday, 7 July 2008, 18:56:50 PM Eastern Daylight Time