CMenu mainMenu; mainMenu.LoadMenu(IDR_MAIN_MENU); CMenu* subMenu = mainMenu.GetSubMenu(0); subMenu->AppendMenu(MF_STRING, ID_FILE_NEW, _T("New")); subMenu->AppendMenu(MF_STRING, ID_FILE_OPEN, _T("Open"));
CMenu contextMenu; contextMenu.LoadMenu(IDR_CONTEXT_MENU); CMenu* subMenu = contextMenu.GetSubMenu(0); subMenu->AppendMenu(MF_STRING, ID_EDIT_PASTE, _T("Paste")); POINT cursorPos; GetCursorPos(&cursorPos); subMenu->TrackPopupMenu(TPM_LEFTALIGN, cursorPos.x, cursorPos.y, AfxGetMainWnd());In this example, a context menu is loaded from a resource file using the LoadMenu function. The GetSubMenu function is called to retrieve the first submenu. The AppendMenu function is used to add a new item to the submenu. The TrackPopupMenu function is used to display the submenu at the cursor position. Package library: MFC (Microsoft Foundation Classes)