Howto: creating a dynamic menu?
-
I would like to create a top menu item with a submenu. I am able to create the topmenu item, but not the submenu unser it. This is used in a SDI app.
void CMyView::CreateOperationMenu() { CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd(); CMenu* pMainMenu = pMainFrame->GetMenu(); // insert the topmenu item if (!pMainMenu->InsertMenu(2, MF_BYPOSITION, 0, "Operation")) return; // get to submenu to add the items CMenu* pSubMenu = pMainMenu->GetSubMenu(2); // ERROR: pSubMenu is always NULL }
Thanks. -
I would like to create a top menu item with a submenu. I am able to create the topmenu item, but not the submenu unser it. This is used in a SDI app.
void CMyView::CreateOperationMenu() { CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd(); CMenu* pMainMenu = pMainFrame->GetMenu(); // insert the topmenu item if (!pMainMenu->InsertMenu(2, MF_BYPOSITION, 0, "Operation")) return; // get to submenu to add the items CMenu* pSubMenu = pMainMenu->GetSubMenu(2); // ERROR: pSubMenu is always NULL }
Thanks.I'm not exactly sure but I think you need to use MF_POPUP for the top menu item, then create a new menu and insert a menu item into it, then call SetMenuItemInfo() on the top menu item with the MIIM_SUBMENU flag. (for regular Win32 programming anyway, it might be a little different for whatever library (MFC/WTL?) you are using...)
-
I'm not exactly sure but I think you need to use MF_POPUP for the top menu item, then create a new menu and insert a menu item into it, then call SetMenuItemInfo() on the top menu item with the MIIM_SUBMENU flag. (for regular Win32 programming anyway, it might be a little different for whatever library (MFC/WTL?) you are using...)
// The code fragment below shows how to create a new menu for the // application window using CreateMenu() and CreatePopupMenu(). // Then, the created menu will replace the current menu of the // application. The old menu will be destroyed with DestroyMenu(). // NOTE: The code fragment below is done in a CFrameWnd-derived class. // Create a new menu for the application window. VERIFY(m_NewMenu.CreateMenu()); // Create a "File" popup menu and insert this popup menu to the // new menu of the application window. The "File" menu has only // one menu item, that is, "Exit". VERIFY(m_FileMenu.CreatePopupMenu()); m_FileMenu.AppendMenu(MF_STRING, ID_APP_EXIT, (LPCTSTR)"E&xit"); m_NewMenu.AppendMenu(MF_POPUP, (UINT) m_FileMenu.m_hMenu, "&File"); // Remove and destroy the old menu. SetMenu(NULL); CMenu* old_menu = CMenu::FromHandle(m_hMenuDefault); old_menu->DestroyMenu(); // Add a new menu. SetMenu(&m_NewMenu); // Assign a default menu. m_hMenuDefault = m_NewMenu.m_hMenu; FAILURE is the first step towards SUCCESS -- modified at 0:50 Saturday 7th January, 2006
-
I would like to create a top menu item with a submenu. I am able to create the topmenu item, but not the submenu unser it. This is used in a SDI app.
void CMyView::CreateOperationMenu() { CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd(); CMenu* pMainMenu = pMainFrame->GetMenu(); // insert the topmenu item if (!pMainMenu->InsertMenu(2, MF_BYPOSITION, 0, "Operation")) return; // get to submenu to add the items CMenu* pSubMenu = pMainMenu->GetSubMenu(2); // ERROR: pSubMenu is always NULL }
Thanks.I posted an article here on the very topic of dynamically building menus. http://www.codeproject.com/menu/DynaMenuToolbar.asp[^] ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
-
I posted an article here on the very topic of dynamically building menus. http://www.codeproject.com/menu/DynaMenuToolbar.asp[^] ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
-
I posted an article here on the very topic of dynamically building menus. http://www.codeproject.com/menu/DynaMenuToolbar.asp[^] ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
One more question (I hope it will be the last one). How can I repaint the menu? When I dynamicly add menu item to the menu, the new menu is draw only when I point the mouse over it. And things get worst why I delete the menu item. The old menu item still paint under the other until the app is hide and show again. I have try to invalidate the mainframe, send WM_PAINT message but none of theses work. Any ideas ? Thanks.
-
One more question (I hope it will be the last one). How can I repaint the menu? When I dynamicly add menu item to the menu, the new menu is draw only when I point the mouse over it. And things get worst why I delete the menu item. The old menu item still paint under the other until the app is hide and show again. I have try to invalidate the mainframe, send WM_PAINT message but none of theses work. Any ideas ? Thanks.
Try removing it and putting it back again. I also seem to remember seeing a function that resets the menu for you (part of the CMenu class?). I'll check a liuttle later. ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -- modified at 18:32 Monday 9th January, 2006