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. I try again - CMenu::AppendMenu

I try again - CMenu::AppendMenu

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
13 Posts 5 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 CoY0te

    Nothing. The problem is, that items with nIDNewItem=0 (created by AppendMenu) are OK, but when i pass parameter greater then 0 to this function these items are disabled. This is the ONLY difference between these items. And more: I can add some items with nIDNewItem=0 and some with nIDNewItrm>0 in the same menu and only those with nIDNewItem>0 will be disabled (this is a popup menu with no submenus). By the way - I've tested some negative ID values - -1000,-2000 and -3000 were enabled and -10000,-20000,-30000 were grayed. Funny thing, but it drives me crazy! Is there any way to check the user's choice without processing the WM_COMMAND message? I didn't notice that CMenu class has any kind of GetSelectedItem method, so I doubt it, but it would be the simpliest way. Anyway thanks for the answer. It's good that someone answered. Greetings to all assembler dudes and especially for C64 demoscene!

    J Offline
    J Offline
    Joaquin M Lopez Munoz
    wrote on last edited by
    #4

    Those IDs have to have some command handler associated with them. Are you processing, for instance, ID_INSERT_ATTRIBUTE in any way? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

    C 1 Reply Last reply
    0
    • J Joaquin M Lopez Munoz

      Those IDs have to have some command handler associated with them. Are you processing, for instance, ID_INSERT_ATTRIBUTE in any way? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

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

      I was supposed to, but I don't do that now since these items are "disabled" - can this really be it? I thought, that if I don't do the processing itself, these items will be available, but they will affect no action when clicked - just that. Do I have to "create" commands somehow?!?!? Right now i'ts only: #define ID_INSERT_ATTRIBUTE And the parent window's OnCommand method is almost empty (processes only ID_INSERT_ATTRIBUTE for testing). Greetings to all assembler dudes and especially for C64 demoscene!

      J 1 Reply Last reply
      0
      • C CoY0te

        I was supposed to, but I don't do that now since these items are "disabled" - can this really be it? I thought, that if I don't do the processing itself, these items will be available, but they will affect no action when clicked - just that. Do I have to "create" commands somehow?!?!? Right now i'ts only: #define ID_INSERT_ATTRIBUTE And the parent window's OnCommand method is almost empty (processes only ID_INSERT_ATTRIBUTE for testing). Greetings to all assembler dudes and especially for C64 demoscene!

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #6

        Yes you have to create those commands. Define them as WM_USER plus some offset of your choice. To implement a handler, take a look at the documentation for ON_COMMAND. Also, you can use FALSE as the second parameter of GfxCmdUIUpdateMenu (this won't make the commands be handled, but at least you'll see the menu entries enabled.) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        C 1 Reply Last reply
        0
        • J Joaquin M Lopez Munoz

          Yes you have to create those commands. Define them as WM_USER plus some offset of your choice. To implement a handler, take a look at the documentation for ON_COMMAND. Also, you can use FALSE as the second parameter of GfxCmdUIUpdateMenu (this won't make the commands be handled, but at least you'll see the menu entries enabled.) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          C Offline
          C Offline
          CoY0te
          wrote on last edited by
          #7

          Didn't work too. I had these commands defined with #define directive. I've changed them to WM_USER+n, and tried it in few different ways - no effect. Both using that function with bDisableIfNoHndler set to TRUE and FALSE and without that function. I'm running out of ideas. I'm gonna get some sleep (it's 23:30 in Poland and I've got back from the pub recently ;) ). Bye. I'll be back tomorrow. Thanks for the effort. Greetings to all assembler dudes and especially for C64 demoscene!

          J 1 Reply Last reply
          0
          • C CoY0te

            Didn't work too. I had these commands defined with #define directive. I've changed them to WM_USER+n, and tried it in few different ways - no effect. Both using that function with bDisableIfNoHndler set to TRUE and FALSE and without that function. I'm running out of ideas. I'm gonna get some sleep (it's 23:30 in Poland and I've got back from the pub recently ;) ). Bye. I'll be back tomorrow. Thanks for the effort. Greetings to all assembler dudes and especially for C64 demoscene!

            J Offline
            J Offline
            Joaquin M Lopez Munoz
            wrote on last edited by
            #8

            Not only do the commands have to be defined, there also must be handlers for them (with ON_COMMAND). As for why bDisableIfNoHndler does not work, I don't know. I guess I'll have to get some sleep too. Good night :) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            J 1 Reply Last reply
            0
            • J Joaquin M Lopez Munoz

              Not only do the commands have to be defined, there also must be handlers for them (with ON_COMMAND). As for why bDisableIfNoHndler does not work, I don't know. I guess I'll have to get some sleep too. Good night :) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

              J Offline
              J Offline
              Jay Beckert
              wrote on last edited by
              #9

              Why not subclass CMenu with CMyCMenu and define the commands and define the handlers in it's message map? Or in your mainframe class define a handler in the message map like: ON_COMMAND(ID_APP_DOTHIS, DoFunction) Would either of those be possible?

              1 Reply Last reply
              0
              • C CoY0te

                If you read this message already - skip it. I just place it here one more time hoping someone will help me. The question is simple, i have the following code for my popup menu: Menu=new CMenu; Menu->CreatePopupMenu(); (...) Menu->AppendMenu(MF_STRING | MF_ENABLED, 0, "Insert"); Menu->AppendMenu(MF_STRING | MF_ENABLED, 0, "Delete"); (...) Menu->TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_LEFTBUTTON, Point.x, Point.y, ParentWindow); Menu->DestroyMenu(); It works ok. But i need to process commands to make it worth anything. When I use: Menu->AppendMenu(MF_STRING | MF_ENABLED, ID_INSERT_ATTRIBUTE, "Insert"); Menu->AppendMenu(MF_STRING | MF_ENABLED, ID_DELETE_ATTRIBUTE, "Delete"); These items appear as grayed and unavailable. I've used CMenu::GetMenuItemID to check if the ID is assigned - it is (returned values were ID_INSERT_ATTRIBUTE and ID_DELETE_ATTRIBUTE). I also used CMenu::EnableMenuItem to enable these (this function returned MF_ENABLED as a previous state of item :confused: ) Did i missed something? By the way - i have problems with CToolTipCtrl - i almost copied all code from one of MSDN's samples but it doesn't work. If someone has some experiences with ToolTips i would like to ask a few questions. [ CoY0te ] ( greetings to all assembler coders :) heh! )

                C Offline
                C Offline
                CoY0te
                wrote on last edited by
                #10

                I guess this conversation led us to a dead end. I'll just start it almost from beginning,cause the problem is unusual for me and from the answers I got, i haven't found out anything yet. Anyway thanks everybody for trying to help me. Here we go: Take a look at my code, and see what is it all about: Version 1: #define ID_EDIT_ATTRIBUTE WM_USER+1 Menu->AppendMenu(MF_STRING | MF_ENABLED, ID_EDIT_ATTRIBUTE, "Edit criterion"); //item is grayed Version 2: Menu->AppendMenu(MF_STRING | MF_ENABLED, 0, "Edit criterion"); //item is not grayed but has no command assigned I've used these two message boxes for testing: if (Menu->GetMenuItemID(0)==ID_EDIT_ATTRIBUTE) MessageBox(NULL, "OK", "Item ID", MB_OK); if (Menu->EnableMenuItem(0, MF_ENABLED | MF_BYPOSITION)==MF_ENABLED) MessageBox(NULL, "OK", "Enabled?", MB_OK); Both of them are appearing before TrackPopupMenu, but "Enabled?" does not appear right after TrackPopupMenu. Conclusion: TrackPopupMenu disables these items, but why does it not accept these command ID's? By the way - I'm using that PopupMenu in a class not derieved from anything (so it's not a CWnd class), but i pass the parent window pointer: Menu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_LEFTBUTTON, Point.x, Point.y, ParentWindow); Right now I'm processing only the ID_EDIT_ATTRIBUTE command in my OnCommand method (just for testing). The question again: Why does it NOT accept my command ID? X| Greetings to all assembler dudes and especially for C64 demoscene!

                M 1 Reply Last reply
                0
                • C CoY0te

                  I guess this conversation led us to a dead end. I'll just start it almost from beginning,cause the problem is unusual for me and from the answers I got, i haven't found out anything yet. Anyway thanks everybody for trying to help me. Here we go: Take a look at my code, and see what is it all about: Version 1: #define ID_EDIT_ATTRIBUTE WM_USER+1 Menu->AppendMenu(MF_STRING | MF_ENABLED, ID_EDIT_ATTRIBUTE, "Edit criterion"); //item is grayed Version 2: Menu->AppendMenu(MF_STRING | MF_ENABLED, 0, "Edit criterion"); //item is not grayed but has no command assigned I've used these two message boxes for testing: if (Menu->GetMenuItemID(0)==ID_EDIT_ATTRIBUTE) MessageBox(NULL, "OK", "Item ID", MB_OK); if (Menu->EnableMenuItem(0, MF_ENABLED | MF_BYPOSITION)==MF_ENABLED) MessageBox(NULL, "OK", "Enabled?", MB_OK); Both of them are appearing before TrackPopupMenu, but "Enabled?" does not appear right after TrackPopupMenu. Conclusion: TrackPopupMenu disables these items, but why does it not accept these command ID's? By the way - I'm using that PopupMenu in a class not derieved from anything (so it's not a CWnd class), but i pass the parent window pointer: Menu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_LEFTBUTTON, Point.x, Point.y, ParentWindow); Right now I'm processing only the ID_EDIT_ATTRIBUTE command in my OnCommand method (just for testing). The question again: Why does it NOT accept my command ID? X| Greetings to all assembler dudes and especially for C64 demoscene!

                  M Offline
                  M Offline
                  Mike Upton
                  wrote on last edited by
                  #11

                  Because, by default, MFC disables menu items that don't have command handlers implemented somewhere in the message map of the parent windows of the menu. You need to add a proper command handler (preferably not in OnCommand, although you can do, you just have to make sure you handle all the parameters properly, including the extra pointer value, and return the correct value) in the window that you specify as a parent in TrackPopupMenu. If you need to change the enabled state of the menu item based on program conditions, you should be using an UPDATE_COMMAND_UI handler as well. For a very detailed explanation of the command routing architecture and command UI update handlers, see Meandering Through the Maze of MFC Message and Command Routing by Paul DiLascia, an excellent article that I've recommended here before.


                  "We are the knights who say Ni" (The Knights Who Say Ni)

                  C 1 Reply Last reply
                  0
                  • M Mike Upton

                    Because, by default, MFC disables menu items that don't have command handlers implemented somewhere in the message map of the parent windows of the menu. You need to add a proper command handler (preferably not in OnCommand, although you can do, you just have to make sure you handle all the parameters properly, including the extra pointer value, and return the correct value) in the window that you specify as a parent in TrackPopupMenu. If you need to change the enabled state of the menu item based on program conditions, you should be using an UPDATE_COMMAND_UI handler as well. For a very detailed explanation of the command routing architecture and command UI update handlers, see Meandering Through the Maze of MFC Message and Command Routing by Paul DiLascia, an excellent article that I've recommended here before.


                    "We are the knights who say Ni" (The Knights Who Say Ni)

                    C Offline
                    C Offline
                    CoY0te
                    wrote on last edited by
                    #12

                    I think I've solved the problem. Thanks everybody for your effort. I appreciate it. My solution: 1. throw out that dynamic creation of menu items 2. create all my popup menus as one resource 3. Use the following: CMenu MenuGroup; MenuGroup.LoadMenu(IDR_POPUP_MENUS); Menu=MenuGroup.GetSubMenu(); Menu->TrackPopupMenu(...); Now I can use the class wizard to handle my menu commands. I still feel strange about these grayed items. I think disabling them should not be the default action when there is no message handler - it would be better (as for me) to leave them enabled and take no action when user clicks them. Well, I guess I'll never understand Mircosoft's way of coding. Thank you very much again. :) :) :) :rose: Greetings to all assembler dudes and especially for C64 demoscene!

                    K 1 Reply Last reply
                    0
                    • C CoY0te

                      I think I've solved the problem. Thanks everybody for your effort. I appreciate it. My solution: 1. throw out that dynamic creation of menu items 2. create all my popup menus as one resource 3. Use the following: CMenu MenuGroup; MenuGroup.LoadMenu(IDR_POPUP_MENUS); Menu=MenuGroup.GetSubMenu(); Menu->TrackPopupMenu(...); Now I can use the class wizard to handle my menu commands. I still feel strange about these grayed items. I think disabling them should not be the default action when there is no message handler - it would be better (as for me) to leave them enabled and take no action when user clicks them. Well, I guess I'll never understand Mircosoft's way of coding. Thank you very much again. :) :) :) :rose: Greetings to all assembler dudes and especially for C64 demoscene!

                      K Offline
                      K Offline
                      KarstenK
                      wrote on last edited by
                      #13

                      It´s better to gray them out, so that even the managers know that it won´t work. ;)

                      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