Adding sub menus to a menu
-
Heres what I have...
{ CMenu menu; menu.CreatePopupMenu(); for (int x = 0; x < ndex; x++) { Swap = StrArr.GetAt(x); if (Swap != "Main") menu.AppendMenu(MF_STRING, x, Swap); } CPoint point; GetCursorPos(&point); SetForegroundWindow(); int Sel = TrackPopupMenuEx(menu.GetSafeHmenu(), TPM_RETURNCMD, point.x, point.y, GetSafeHwnd(), NULL); PostMessage(WM_NULL, 0, 0); menu.DestroyMenu(); }
I've deleted variable declarations and some code to save space. With the AppendMenu(), I'm of course adding menu items. What I want to do is add sub menus to each of these menu items. Instead of clicking on an item and executing a function, I want a sub menu to pop up. Can't figure out how to do it. Any help? Thanks! Daniel -
Heres what I have...
{ CMenu menu; menu.CreatePopupMenu(); for (int x = 0; x < ndex; x++) { Swap = StrArr.GetAt(x); if (Swap != "Main") menu.AppendMenu(MF_STRING, x, Swap); } CPoint point; GetCursorPos(&point); SetForegroundWindow(); int Sel = TrackPopupMenuEx(menu.GetSafeHmenu(), TPM_RETURNCMD, point.x, point.y, GetSafeHwnd(), NULL); PostMessage(WM_NULL, 0, 0); menu.DestroyMenu(); }
I've deleted variable declarations and some code to save space. With the AppendMenu(), I'm of course adding menu items. What I want to do is add sub menus to each of these menu items. Instead of clicking on an item and executing a function, I want a sub menu to pop up. Can't figure out how to do it. Any help? Thanks! DanielThe menu objects are always grouped as sub or pop-up menus. Now, considering your code post, there is one crucial difference in creating pop-up menus. Firstly, you need to create a new
CMenu
object. Then initialize it withCreatePopupMenu
like you have done. After that, you need to create a yet anotherCMenu
item, and initialize it withCreatePopupMenu
again. The second menu object behaves like a pop-up menu on the first one. In order to link these two, first add items and their respective command IDs to the second menu by usingAppendMenu
orInsertMenu
. Then useAppendMenu
/InsertMenu
on the first menu object, and specifyMF_POPUP
flag in the first parameter. Adding aMF_STRING
flag allows you to have an item text as well, specified in the third parameter. Instead of a command ID in the second parameter, you now must supply a handle to the sub-menu object (HMENU
). You can get this from the second menu object by queryingCMenu::operator HMENU
orCMenu::m_hMenu
. After this, you can launch the first menu object as a pop-up menu with a sub pop-up by callingTrackPopupMenuEx
. Here is a complete code fragment to give you a better clue:// Create a CMenu object and initialize
CMenu* pMenu = new CMenu();
pMenu->CreatePopupMenu();// Create the sub pop-up menu object
CMenu* pSubMenu = new CMenu();
pSubMenu->CreatePopupMenu();// Add menu items into the second menu
pSubMenu->AppendMenu( MF_STRING, 1, "Option 1" );
pSubMenu->AppendMenu( MF_STRING, 2, "Option 2" );// Add the pop-up menu
pMenu->AppendMenu( MF_STRING | MF_POPUP, (UINT_PTR) pSubMenu->m_hMenu, "Sub menu" );// Launch and do cleanup
pMenu->TrackPopupMenuEx(...);pMenu->RemoveMenu( 0, MF_BYPOSITION );
pSubMenu->DestroyMenu(); delete pSubMenu; pSubMenu = NULL;
pMenu->DestroyMenu(); delete pMenu; pMenu = NULL;I am not certain if the
RemoveMenu
call is necessary, as the menu cleanup code might have a routine that destroys sub-menus as well. Also, the code has no error-checking routines for possible error states like