// Create a new menu item with a sub-menu. CMenu submenu; submenu.CreatePopupMenu(); submenu.AppendMenu(MF_STRING, ID_SUBMENU_ITEM1, _T("Submenu Item 1")); submenu.AppendMenu(MF_STRING, ID_SUBMENU_ITEM2, _T("Submenu Item 2")); // Add the new sub-menu to an existing menu. CMenu* mainMenu = AfxGetMainWnd()->GetMenu(); mainMenu->AppendMenu(MF_POPUP, (UINT_PTR)submenu.m_hMenu, _T("Submenu"));
// Add a separator to an existing menu. CMenu* mainMenu = AfxGetMainWnd()->GetMenu(); mainMenu->AppendMenu(MF_SEPARATOR); // Add a new menu item after the separator. mainMenu->AppendMenu(MF_STRING, ID_ITEM1, _T("Item 1"));In both examples, the CMenu object is used to create a new sub-menu or separator. The AppendMenu function is then called on the main menu (obtained using the AfxGetMainWnd() function) to add the new sub-menu or separator to the existing menu. The first parameter of AppendMenu specifies the type of menu item to add (MF_POPUP for a sub-menu, MF_SEPARATOR for a separator, and MF_STRING for a regular item), followed by other parameters such as the menu ID and the text to display on the new item. The package library used in these examples is the Microsoft Foundation Classes (MFC) library.