|
1 | 1 | --- |
2 | 2 | title: "Creating Pop-up Menus (C++)" |
3 | 3 | ms.date: "11/04/2016" |
4 | | -helpviewer_keywords: ["context menus [C++], Menu Editor", "pop-up menus [C++], creating", "menus [C++], pop-up", "menus [C++], creating", "shortcut menus [C++], creating", "pop-up menus [C++], displaying"] |
| 4 | +helpviewer_keywords: ["context menus [C++], Menu Editor", "pop-up menus [C++], creating", "menus [C++], pop-up", "menus [C++], creating", "shortcut menus [C++], creating", "pop-up menus [C++], displaying", "pop-up menus [C++], connecting to applications", "context menus [C++], connecting to applications", "shortcut menus [C++], connecting to applications", "pop-up menus"] |
5 | 5 | ms.assetid: dff4dddf-2e8d-4c34-b755-90baae426b58 |
6 | 6 | --- |
7 | 7 | # Creating Pop-up Menus (C++) |
8 | 8 |
|
9 | 9 | [Pop-up menus](../mfc/menus-mfc.md) display frequently used commands. 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. |
10 | 10 |
|
11 | | -Once you have created the menu resource, your application code needs to load the menu resource and use [TrackPopupMenu](/windows/desktop/api/winuser/nf-winuser-trackpopupmenu) 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. |
| 11 | +Once you've created the menu resource, your application code needs to load the menu resource and use [TrackPopupMenu](/windows/desktop/api/winuser/nf-winuser-trackpopupmenu) to cause the menu to appear. Once the user has dismissed the pop-up menu by selecting outside it, or has selected 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. |
12 | 12 |
|
13 | | -### To create a pop-up menu |
| 13 | +## To create a pop-up menu |
14 | 14 |
|
15 | 15 | 1. [Create a menu](../windows/creating-a-menu.md) with an empty title (don't provide a **Caption**). |
16 | 16 |
|
17 | | -2. [Add a menu command to the new menu](../windows/adding-commands-to-a-menu.md). Move to the first menu command below the blank menu title (the temporary caption says `Type Here`). Type a **Caption** and any other information. |
| 17 | +1. [Add a menu command to the new menu](../windows/adding-commands-to-a-menu.md). Move to the first menu command below the blank menu title (the temporary caption says `Type Here`). Type a **Caption** and any other information. |
18 | 18 |
|
19 | 19 | Repeat this process for any other menu commands in the pop-up menu. |
20 | 20 |
|
21 | | -3. Save the menu resource. |
| 21 | +1. Save the menu resource. |
22 | 22 |
|
23 | | - > [!TIP] |
24 | | - > For more information on viewing the pop-up menu, see [Viewing a Menu as a Pop-Up Menu](../windows/viewing-a-menu-as-a-pop-up-menu.md). |
| 23 | +## To connect a pop-up menu to your application |
| 24 | + |
| 25 | +1. Add a message handler for WM_CONTEXTMENU (for example). For more information, see [Mapping Messages to Functions](../mfc/reference/mapping-messages-to-functions.md). |
| 26 | + |
| 27 | +1. Add the following code to the message handler: |
| 28 | + |
| 29 | + ```cpp |
| 30 | + CMenu menu; |
| 31 | + VERIFY(menu.LoadMenu(IDR_MENU1)); |
| 32 | + CMenu* pPopup = menu.GetSubMenu(0); |
| 33 | + ASSERT(pPopup != NULL); |
| 34 | + pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd()); |
| 35 | + ``` |
| 36 | + |
| 37 | + > [!NOTE] |
| 38 | + > The [CPoint](../atl-mfc-shared/reference/cpoint-class.md) passed by the message handler is in screen coordinates. |
| 39 | +
|
| 40 | + > [!NOTE] |
| 41 | + > Connecting a pop-up menu to your application requires MFC. |
| 42 | +
|
| 43 | +## To view a menu resource as a pop-up menu |
| 44 | + |
| 45 | +Normally, when you're working in the **Menu** editor, a menu resource is displayed as a menu bar. However, you might have menu resources that are added to the application's menu bar while the program is running. |
| 46 | + |
| 47 | +Right-click the menu and choose **View as Popup** from the shortcut menu. |
| 48 | + |
| 49 | + This option is only a viewing preference and won't modify your menu. |
| 50 | + |
| 51 | + > [!NOTE] |
| 52 | + > To change back to the menu-bar view, click **View as Popup** again (which removes the check mark and returns your menu-bar view). |
25 | 53 |
|
26 | 54 | ## Requirements |
27 | 55 |
|
28 | 56 | Win32 |
29 | 57 |
|
30 | 58 | ## See Also |
31 | 59 |
|
32 | | -[Connecting a Pop-up Menu to Your Application](../windows/connecting-a-pop-up-menu-to-your-application.md)<br/> |
33 | | -[Menu Editor](../windows/menu-editor.md) |
| 60 | +[Menu Editor](../windows/menu-editor.md) |
0 commit comments