|
1 | 1 | ---
|
2 |
| -title: "Creating a Menu (C++)" |
| 2 | +title: "Creating Menus (C++)" |
3 | 3 | ms.date: "11/04/2016"
|
4 | 4 | f1_keywords: ["vc.editors.menu"]
|
5 |
| -helpviewer_keywords: ["mnemonics [C++], associating menu items", "menus [C++], associating commands with mnemonic keys", "menus [C++], creating", "menus [C++], adding items", "commands [C++], adding to menus", "menu items, adding to menus"] |
| 5 | +helpviewer_keywords: ["mnemonics [C++], associating menu items", "menus [C++], associating commands with mnemonic keys", "menus [C++], creating", "menus [C++], adding items", "commands [C++], adding to menus", "menu items, adding to menus", "submenus", "submenus [C++], creating", "menus [C++], creating", "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"] |
6 | 6 | ms.assetid: 66f94448-9b97-4b73-bf97-10d4bf87cc65
|
7 | 7 | ---
|
8 |
| -# Creating a Menu (C++) |
| 8 | +# Creating Menus (C++) |
9 | 9 |
|
10 | 10 | > [!NOTE]
|
11 | 11 | > The **Resource Window** is not available in Express editions.
|
@@ -33,6 +33,14 @@ For information on adding resources to managed projects, see [Resources in Deskt
|
33 | 33 | > [!NOTE]
|
34 | 34 | > To create a single-item menu on the menu bar, set the **Popup** property to **False**.
|
35 | 35 |
|
| 36 | +## To create a submenu |
| 37 | + |
| 38 | +1. Select the menu command for which you want to create a submenu. |
| 39 | + |
| 40 | +1. In the **New Item** box that appears to the right, type the name of the new menu command. This new command will appear first on the submenu menu. |
| 41 | + |
| 42 | +1. Add additional menu commands to the submenu menu. |
| 43 | + |
36 | 44 | ## To insert a new menu between existing menus
|
37 | 45 |
|
38 | 46 | Select an existing menu name and press the **Insert** key. The **New Item** box is inserted before the selected item.
|
@@ -70,10 +78,57 @@ Right-click on the menu bar and choose **Insert New** from the shortcut menu.
|
70 | 78 |
|
71 | 79 | The new item box is selected so you can create additional menu commands.
|
72 | 80 |
|
| 81 | +## To create pop-up menus |
| 82 | + |
| 83 | +[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. |
| 84 | + |
| 85 | +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. |
| 86 | + |
| 87 | +### To create a pop-up menu |
| 88 | + |
| 89 | +1. [Create a menu](../windows/creating-a-menu.md) with an empty title (don't provide a **Caption**). |
| 90 | + |
| 91 | +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. |
| 92 | + |
| 93 | + Repeat this process for any other menu commands in the pop-up menu. |
| 94 | + |
| 95 | +1. Save the menu resource. |
| 96 | + |
| 97 | +### To connect a pop-up menu to your application |
| 98 | + |
| 99 | +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). |
| 100 | + |
| 101 | +1. Add the following code to the message handler: |
| 102 | + |
| 103 | + ```cpp |
| 104 | + CMenu menu; |
| 105 | + VERIFY(menu.LoadMenu(IDR_MENU1)); |
| 106 | + CMenu* pPopup = menu.GetSubMenu(0); |
| 107 | + ASSERT(pPopup != NULL); |
| 108 | + pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd()); |
| 109 | + ``` |
| 110 | + |
| 111 | + > [!NOTE] |
| 112 | + > The [CPoint](../atl-mfc-shared/reference/cpoint-class.md) passed by the message handler is in screen coordinates. |
| 113 | +
|
| 114 | + > [!NOTE] |
| 115 | + > Connecting a pop-up menu to your application requires MFC. |
| 116 | +
|
| 117 | +### To view a menu resource as a pop-up menu |
| 118 | + |
| 119 | +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. |
| 120 | + |
| 121 | +Right-click the menu and choose **View as Popup** from the shortcut menu. |
| 122 | + |
| 123 | + This option is only a viewing preference and won't modify your menu. |
| 124 | + |
| 125 | + > [!NOTE] |
| 126 | + > To change back to the menu-bar view, click **View as Popup** again (which removes the check mark and returns your menu-bar view). |
| 127 | +
|
73 | 128 | ## Requirements
|
74 | 129 |
|
75 | 130 | Win32
|
76 | 131 |
|
77 |
| -## See Also |
| 132 | +## See also |
78 | 133 |
|
79 | 134 | [Menu Editor](../windows/menu-editor.md)
|
0 commit comments