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

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


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