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. Dynamic Menu Creation

Dynamic Menu Creation

Scheduled Pinned Locked Moved C / C++ / MFC
tutoriallearning
9 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.
  • T Offline
    T Offline
    Thakur JAI SINGH
    wrote on last edited by
    #1

    Hi I have created a menu using Resource. for example--> A and below I want to add one menu named B dynamically. How to do that.

    _ 1 Reply Last reply
    0
    • T Thakur JAI SINGH

      Hi I have created a menu using Resource. for example--> A and below I want to add one menu named B dynamically. How to do that.

      _ Offline
      _ Offline
      _Flaviu
      wrote on last edited by
      #2

      CMenu menu;
      menu.LoadMenu(IDR_RESOURCEMENU);
      CMenu* pContextMenu = menu.GetSubMenu(1);

      pContextMenu->InsertMenu(0, MF\_STRING | MF\_BYPOSITION, ID\_LISTMENU\_OPEN, \_T("&Open"));
      pContextMenu->InsertMenu(1, MF\_SEPARATOR | MF\_BYPOSITION, 0); // insert your menu at index 0 (first position)
      pContextMenu->SetDefaultItem(0, TRUE); // put the inserted menu as default
      

      ID_LISTMENU_OPEN must declared somewhere in your app, as UINT Hope it help you ...

      T 1 Reply Last reply
      0
      • _ _Flaviu

        CMenu menu;
        menu.LoadMenu(IDR_RESOURCEMENU);
        CMenu* pContextMenu = menu.GetSubMenu(1);

        pContextMenu->InsertMenu(0, MF\_STRING | MF\_BYPOSITION, ID\_LISTMENU\_OPEN, \_T("&Open"));
        pContextMenu->InsertMenu(1, MF\_SEPARATOR | MF\_BYPOSITION, 0); // insert your menu at index 0 (first position)
        pContextMenu->SetDefaultItem(0, TRUE); // put the inserted menu as default
        

        ID_LISTMENU_OPEN must declared somewhere in your app, as UINT Hope it help you ...

        T Offline
        T Offline
        Thakur JAI SINGH
        wrote on last edited by
        #3

        @Flaviuu Thanx for the reply Its not working for me void CmainWn::OnBZ() { // TODO: Add your command handler code here CMenu menu; menu.LoadMenuA(IDR_MENU1); CMenu* pcontextmenu=menu.GetSubMenu(0); pcontextmenu->InsertMenuA (0,MF_STRING|MF_BYPOSITION,ID_SHOW,_T("&Open")); pcontextmenu->InsertMenuA(1,MF_SEPARATOR|MF_BYPOSITION,0); pcontextmenu->SetDefaultItem(0,TRUE); } I added this code on the event handler of menu Z which is under menu B at location 0. But it is not working for me. My objective is here is on the click of menu Z one menu should be added in my main menu(IDR_MENU1) named as A Correct me If I am wrong somewhere.

        _ D 2 Replies Last reply
        0
        • T Thakur JAI SINGH

          @Flaviuu Thanx for the reply Its not working for me void CmainWn::OnBZ() { // TODO: Add your command handler code here CMenu menu; menu.LoadMenuA(IDR_MENU1); CMenu* pcontextmenu=menu.GetSubMenu(0); pcontextmenu->InsertMenuA (0,MF_STRING|MF_BYPOSITION,ID_SHOW,_T("&Open")); pcontextmenu->InsertMenuA(1,MF_SEPARATOR|MF_BYPOSITION,0); pcontextmenu->SetDefaultItem(0,TRUE); } I added this code on the event handler of menu Z which is under menu B at location 0. But it is not working for me. My objective is here is on the click of menu Z one menu should be added in my main menu(IDR_MENU1) named as A Correct me If I am wrong somewhere.

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #4

          You should explain better: what you call "Menu Z" ? If I understand well, you have a resource menu, IDR_MENU1, where you want to add dynamically an menu item, ID_SHOW ... I am right ? P.S. If you post code, use tag ...

          T 1 Reply Last reply
          0
          • _ _Flaviu

            You should explain better: what you call "Menu Z" ? If I understand well, you have a resource menu, IDR_MENU1, where you want to add dynamically an menu item, ID_SHOW ... I am right ? P.S. If you post code, use tag ...

            T Offline
            T Offline
            Thakur JAI SINGH
            wrote on last edited by
            #5

            @Flaviuu I have defined Menu Z its entry in resource file is ID_B_Z . Yes you are right I am having a resource menu IDR_MENU1 where I want to add a menu named ID_SHOW dynamically. I just want to know what is wrong with my code ?

            _ 1 Reply Last reply
            0
            • T Thakur JAI SINGH

              @Flaviuu I have defined Menu Z its entry in resource file is ID_B_Z . Yes you are right I am having a resource menu IDR_MENU1 where I want to add a menu named ID_SHOW dynamically. I just want to know what is wrong with my code ?

              _ Offline
              _ Offline
              _Flaviu
              wrote on last edited by
              #6

              How you defined ID_SHOW ?

              T 1 Reply Last reply
              0
              • _ _Flaviu

                How you defined ID_SHOW ?

                T Offline
                T Offline
                Thakur JAI SINGH
                wrote on last edited by
                #7

                @Flaviuu Yes I have defined it in my Resource.h file like this. #define ID_SHOW 107

                F 1 Reply Last reply
                0
                • T Thakur JAI SINGH

                  @Flaviuu Yes I have defined it in my Resource.h file like this. #define ID_SHOW 107

                  F Offline
                  F Offline
                  Frankie C
                  wrote on last edited by
                  #8

                  Use CMenu::InsertMenuItem that allows you to insert a menu item, an image, a submenu or whatever you want. See[^].

                  1 Reply Last reply
                  0
                  • T Thakur JAI SINGH

                    @Flaviuu Thanx for the reply Its not working for me void CmainWn::OnBZ() { // TODO: Add your command handler code here CMenu menu; menu.LoadMenuA(IDR_MENU1); CMenu* pcontextmenu=menu.GetSubMenu(0); pcontextmenu->InsertMenuA (0,MF_STRING|MF_BYPOSITION,ID_SHOW,_T("&Open")); pcontextmenu->InsertMenuA(1,MF_SEPARATOR|MF_BYPOSITION,0); pcontextmenu->SetDefaultItem(0,TRUE); } I added this code on the event handler of menu Z which is under menu B at location 0. But it is not working for me. My objective is here is on the click of menu Z one menu should be added in my main menu(IDR_MENU1) named as A Correct me If I am wrong somewhere.

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Thakur JAI SINGH wrote:

                    But it is not working for me.

                    What specifically is not working? You have 5 statements here. Have you stepped through each one using the debugger to see which one is not working as expected?

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                    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