How do I create popup submenus dynamically
-
I am finding it impossible to create a popup submenu dynamically inside an existing popup menu (from a template) and then populate it with menu items. I'm trying to modify the tempalte menus inside the CMainFrame::OnInitMenuPopup() handler. I've been through all the CMenu documentation and can't figure out the trick to this. I can insert and append menu items to my heart's content, but not popups! Is there sample code somewhere for doing this sort of thing? More details: I can successfully create a submenu entry in the popup using CMenu::InsertMenu(..., MF_POPUP, ...) but there is no right arrow in the menu. Also, when I open the popup menu a second time, the submenu entry is grayed out. So I don't seem to be able to begin the submenu successfully, nor can I populate it. I would guess that populating the submenu would also be done by OnInitMenuPopup() when it was called (*if* I could get an arrow on the submenu entry!). A big question here is then how I could know that it is my submenu being passed into OnInitMenuPopup() since there are not any commands yet defined to test against. I was thinking of saving the m_hMenu when I created the submenu and then checking against the CMenu->m_hMenu passed into OnInitMenuPopup()? But this seems a little kludgy. I'm certainly not a "beginner" and I can't figure it out from the docs I've found. Why did they make it this hard? Mike Newberry
-
I am finding it impossible to create a popup submenu dynamically inside an existing popup menu (from a template) and then populate it with menu items. I'm trying to modify the tempalte menus inside the CMainFrame::OnInitMenuPopup() handler. I've been through all the CMenu documentation and can't figure out the trick to this. I can insert and append menu items to my heart's content, but not popups! Is there sample code somewhere for doing this sort of thing? More details: I can successfully create a submenu entry in the popup using CMenu::InsertMenu(..., MF_POPUP, ...) but there is no right arrow in the menu. Also, when I open the popup menu a second time, the submenu entry is grayed out. So I don't seem to be able to begin the submenu successfully, nor can I populate it. I would guess that populating the submenu would also be done by OnInitMenuPopup() when it was called (*if* I could get an arrow on the submenu entry!). A big question here is then how I could know that it is my submenu being passed into OnInitMenuPopup() since there are not any commands yet defined to test against. I was thinking of saving the m_hMenu when I created the submenu and then checking against the CMenu->m_hMenu passed into OnInitMenuPopup()? But this seems a little kludgy. I'm certainly not a "beginner" and I can't figure it out from the docs I've found. Why did they make it this hard? Mike Newberry
Hi Mike, Perhaps this will be of interest to you. I've once made a DLL to be used in CodeWright. This DLL was adding a menu item to an existing menu bar (the one of CodeWright itself). After popupating the pull down menu of the new menu, I added some popup items, which I think is also what you want to do. I've included here some code snippets. Hope they are useful to you ;-). First of all, I'm trying to get a pointer to the own made menu item, which I need to start building up the popup menu's. After that, I'm having a temp variable (pointer) which I'm using to build several submenu's in the pull down menu. I've added some comments to make it more clear of what has been done. Hope it's clear enough. I'm using this 'tool' without any problems and the popup menu's are working fine, even in an applications of which the base is not my code (in this case here, it's CodeWright in which I had to fit this mechanism). B.t.w.: even Premia (the company who makes the CodeWright editor), they couldn't give me a clear answer on what I wanted to do, so after many hours of trying and struggling, I finally found it myself... As you can see from the code snippet, also graying or check-marking the popup menu item is in (and as such, not so difficult to do ;-) )... /////////////////////////////////////////////////////////////////////////////// // Get a pointer to the menu of which we want to change an item. // 'pTestMenu' is a pointer to a menu item of a main menu. After retrieving this pointer, // we can add/change a submenu to this item. // int temp_location = 0; CString temp_string; m_pWnd->GetMenu()->GetMenuString( temp_location, temp_string, MF_BYPOSITION ); while ( temp_string != "&Gvc" ) { m_pWnd->GetMenu()->GetMenuString( ++temp_location, temp_string, MF_BYPOSITION ); } // Get the pointer to the own-made menu item CMenu* pTestMenu = m_pWnd->GetMenu()->GetSubMenu( temp_location ); // Make a temp. variable of the type CMenu, to create the cascading menu 'on the fly' CMenu menuPopup; //////////////////////////////////////////////////////////////////////// // Creation of the popup menu for the Clean selection: "Target" or "All" // menuPopup.CreatePopupMenu(); menuPopup.AppendMenu( MF_STRING , ID_GVCMENU_CLEAN_TARGET , "Only target items" ); menuPopup.AppendMenu( MF_STRING , ID_G
-
Hi Mike, Perhaps this will be of interest to you. I've once made a DLL to be used in CodeWright. This DLL was adding a menu item to an existing menu bar (the one of CodeWright itself). After popupating the pull down menu of the new menu, I added some popup items, which I think is also what you want to do. I've included here some code snippets. Hope they are useful to you ;-). First of all, I'm trying to get a pointer to the own made menu item, which I need to start building up the popup menu's. After that, I'm having a temp variable (pointer) which I'm using to build several submenu's in the pull down menu. I've added some comments to make it more clear of what has been done. Hope it's clear enough. I'm using this 'tool' without any problems and the popup menu's are working fine, even in an applications of which the base is not my code (in this case here, it's CodeWright in which I had to fit this mechanism). B.t.w.: even Premia (the company who makes the CodeWright editor), they couldn't give me a clear answer on what I wanted to do, so after many hours of trying and struggling, I finally found it myself... As you can see from the code snippet, also graying or check-marking the popup menu item is in (and as such, not so difficult to do ;-) )... /////////////////////////////////////////////////////////////////////////////// // Get a pointer to the menu of which we want to change an item. // 'pTestMenu' is a pointer to a menu item of a main menu. After retrieving this pointer, // we can add/change a submenu to this item. // int temp_location = 0; CString temp_string; m_pWnd->GetMenu()->GetMenuString( temp_location, temp_string, MF_BYPOSITION ); while ( temp_string != "&Gvc" ) { m_pWnd->GetMenu()->GetMenuString( ++temp_location, temp_string, MF_BYPOSITION ); } // Get the pointer to the own-made menu item CMenu* pTestMenu = m_pWnd->GetMenu()->GetSubMenu( temp_location ); // Make a temp. variable of the type CMenu, to create the cascading menu 'on the fly' CMenu menuPopup; //////////////////////////////////////////////////////////////////////// // Creation of the popup menu for the Clean selection: "Target" or "All" // menuPopup.CreatePopupMenu(); menuPopup.AppendMenu( MF_STRING , ID_GVCMENU_CLEAN_TARGET , "Only target items" ); menuPopup.AppendMenu( MF_STRING , ID_G
Hi again, Mike, I've seen I misspelled one (or more???) word(s): 'popupating' should of course be 'populating', but since I'm neither English nor American, so my English is not so perfect ;-) Also, I've seen that the processed code (after submitting the article) is removing tabs and/or spaces, so the code snippet is sometimes hard to read. Sorry for that, but that's out of my control... Regards, Geert