Creating Pop-up Menus

OverviewHow Do I

Pop-up menus (also known as shortcut menus) display frequently-used commands with a right mouse click. They can be context sensitive to the location of the pointer. Using pop-up menus in your application requires building the menu itself and then connecting it to application code.

Once you have created the menu resource, your application code needs to load the menu resource and use the TrackPopupMenu command to cause the menu to appear. Once the user has dismissed the pop-up menu by clicking outside it, or has clicked on a command, that function will return. If the user chooses a command, that command message will be sent to the window whose handle was passed.

What do you want to do?

  • Create a pop-up menu

  • Connect a pop-up menu to your application

To create a pop-up menu

  1. Create a menu bar with an empty title.

  2. Type a temporary letter in the caption or choose an attribute to reverse later. This enables the menu to be created.

  3. Move to the first menu item in the menu. Bring up the property page and type in the caption and any other information.

    Repeat this process for any other menu items in the pop-up menu.

  4. Make the top menu bar empty again by deleting the temporary letter in the caption or by resetting the temporary attribute.

    The goal is to have a pop-up menu descending beneath a blank menu bar.

  5. Save the menu resource.

To connect a pop-up menu to your application

  • Add the following code to your source file:

     CMenu menu;
     VERIFY(menu.LoadMenu(IDR_MENU1));
     CMenu* pPopup = menu.GetSubMenu(0);
     ASSERT(pPopup != NULL);
    
     pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, AfxGetMainWnd());
    

See Also   , , , , , ,