Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Displays a floating pop-up menu at the specified location and tracks the selection of items on the pop-up menu.

BOOL TrackPopupMenu(
   UINT nFlags,
   int x,
   int y,
   CWnd* pWnd,
   LPCRECT lpRect = 0


Specifies a screen-position flag and a mouse-button flag. The screen-position flag can be one of the following:
  • TPM_CENTERALIGN   Centers the pop-up menu horizontally relative to the coordinate specified by x.
  • TPM_LEFTALIGN   Positions the pop-up menu so that its left side is aligned with the coordinate specified by x.
  • TPM_RIGHTALIGN   Positions the pop-up menu so that its right side is aligned with the coordinate specified by x.

The mouse-button flag can be either of the following:

  • TPM_LEFTBUTTON   Causes the pop-up menu to track the left mouse button.
  • TPM_RIGHTBUTTON   Causes the pop-up menu to track the right mouse button.
Specifies the horizontal position in screen coordinates of the pop-up menu. Depending on the value of the nFlags parameter, the menu can be left-aligned, right-aligned, or centered relative to this position.
Specifies the vertical position in screen coordinates of the top of the menu on the screen.
Identifies the window that owns the pop-up menu. This window receives all WM_COMMAND messages from the menu. In Windows versions 3.1 and later, the window does not receive WM_COMMAND messages until TrackPopupMenu returns. In Windows 3.0, the window receives WM_COMMAND messages before TrackPopupMenu returns.
Points to a RECT structure or CRect object that contains the screen coordinates of a rectangle within which the user can click without dismissing the pop-up menu. If this parameter is 0, the pop-up menu is dismissed if the user clicks outside the pop-up menu. This must be 0 for Windows 3.0.

For Windows 3.1 and later, you can use the following constants:


Return Value

Nonzero if the function is successful; otherwise 0.


A floating pop-up menu can appear anywhere on the screen.


// The code fragment shows how to get the File menu from the
// application window and displays it as a floating popup menu
// when the right mouse button is clicked in view.
// CMyView is a CView-derived class.
void CMyView::OnRButtonDown(UINT nFlags, CPoint point) 
   CView::OnRButtonDown(nFlags, point);

   CMenu* menu_bar = AfxGetMainWnd()->GetMenu();
   CMenu* file_menu = menu_bar->GetSubMenu(0);    

   file_menu->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, point.x, 
      point.y, this);

See Also

CMenu Overview | Class Members | Hierarchy Chart | CMenu::CreatePopupMenu | CMenu::GetSubMenu | TrackPopupMenu

© 2015 Microsoft