Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Howto: creating a dynamic menu?

Howto: creating a dynamic menu?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cromag
    wrote on last edited by
    #1

    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.

    N realJSOPR 2 Replies Last reply
    0
    • C cromag

      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.

      N Offline
      N Offline
      nm_114
      wrote on last edited by
      #2

      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...)

      L 1 Reply Last reply
      0
      • N nm_114

        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...)

        L Offline
        L Offline
        Laxman9
        wrote on last edited by
        #3

        // 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

        1 Reply Last reply
        0
        • C cromag

          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.

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          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

          C 2 Replies Last reply
          0
          • realJSOPR realJSOP

            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

            C Offline
            C Offline
            cromag
            wrote on last edited by
            #5

            Thanks!

            1 Reply Last reply
            0
            • realJSOPR realJSOP

              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

              C Offline
              C Offline
              cromag
              wrote on last edited by
              #6

              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.

              realJSOPR 1 Reply Last reply
              0
              • C cromag

                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.

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups