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. OnCmdMsg Question

OnCmdMsg Question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
11 Posts 3 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.
  • Y Offline
    Y Offline
    yellowine
    wrote on last edited by
    #1

    In MFC Doc/View SDI or MDI application, every command message sent by menu items or buttons in a toolbar can be routed to CMainFram::OnCmdMsg(..) function. Howevey, if I want to menually send a command message using: SendMessage(AfxGetMainWnd(), WM_COMMAND, ID_MYCOMMAND, 0); or using PostMessage(AfxGetMainWnd(), WM_COMMAND, ID_MYCOMMAND, 0); this message doesn't get routed to CMainFram::OnCmdMsg(..). Why? In other words, how can I simulate the menu command message using SendMessage or PostMessage?

    RaviBeeR 1 Reply Last reply
    0
    • Y yellowine

      In MFC Doc/View SDI or MDI application, every command message sent by menu items or buttons in a toolbar can be routed to CMainFram::OnCmdMsg(..) function. Howevey, if I want to menually send a command message using: SendMessage(AfxGetMainWnd(), WM_COMMAND, ID_MYCOMMAND, 0); or using PostMessage(AfxGetMainWnd(), WM_COMMAND, ID_MYCOMMAND, 0); this message doesn't get routed to CMainFram::OnCmdMsg(..). Why? In other words, how can I simulate the menu command message using SendMessage or PostMessage?

      RaviBeeR Offline
      RaviBeeR Offline
      RaviBee
      wrote on last edited by
      #2

      You're sending/posting the message correctly. However, you need a handler of the form:

      ON_COMMAND(ID_MYCOMMAND, OnMyCommand)

      /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

      Y 1 Reply Last reply
      0
      • RaviBeeR RaviBee

        You're sending/posting the message correctly. However, you need a handler of the form:

        ON_COMMAND(ID_MYCOMMAND, OnMyCommand)

        /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

        Y Offline
        Y Offline
        yellowine
        wrote on last edited by
        #3

        Thanks. I did have the command handler. The message sent by SendMessage or PostMessage did get routed to CMainFrame::OnCommand(...) function not CMainFrame::OnCmdMsg(...). But the message sent by a menu item gets routed to CMainFrame::OnCmdMsg(...). What I am wondering is that the difference between those two mechanism.

        RaviBeeR 1 Reply Last reply
        0
        • Y yellowine

          Thanks. I did have the command handler. The message sent by SendMessage or PostMessage did get routed to CMainFrame::OnCommand(...) function not CMainFrame::OnCmdMsg(...). But the message sent by a menu item gets routed to CMainFrame::OnCmdMsg(...). What I am wondering is that the difference between those two mechanism.

          RaviBeeR Offline
          RaviBeeR Offline
          RaviBee
          wrote on last edited by
          #4

          Hmmm, that's odd. The command handler for IDC_MYCOMMAND should get called for the menu item IDC_MYCOMMAND. Perhaps the notification isn't being passed down the chain. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

          Y 1 Reply Last reply
          0
          • RaviBeeR RaviBee

            Hmmm, that's odd. The command handler for IDC_MYCOMMAND should get called for the menu item IDC_MYCOMMAND. Perhaps the notification isn't being passed down the chain. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

            Y Offline
            Y Offline
            yellowine
            wrote on last edited by
            #5

            The command handler gets called actually for either mechanism but it seems so via different road. Menu command thru OnCmdMessage, and mannually sent command thru OnCommand. The reason I ask this question is that the command handler may be located in a View or Doc class, not necessary in CMainFrame class, if the menually sent command message is routed to CMainFrame::OnCmdMsg function, then I had the chance to forward the message to other classes capable of handling message.

            S 1 Reply Last reply
            0
            • Y yellowine

              The command handler gets called actually for either mechanism but it seems so via different road. Menu command thru OnCmdMessage, and mannually sent command thru OnCommand. The reason I ask this question is that the command handler may be located in a View or Doc class, not necessary in CMainFrame class, if the menually sent command message is routed to CMainFrame::OnCmdMsg function, then I had the chance to forward the message to other classes capable of handling message.

              S Offline
              S Offline
              Steen Krogsgaard
              wrote on last edited by
              #6

              as far as I can see all WM_COMMAND messages gets routed to the receiving windows' OnCommand handler, which in turn calls OnCmdMsg (provided some prerequisites are present). Call stack: CCmdTarget::OnCmdMsg CWinApp::OnCmdMsg CWnd::OnCmdMsg CView::OnCmdMsg CFrameWnd::OnCmdMsg CWnd::OnCommand CFrameWnd::OnCommand CWnd::OnWndMsg CWnd::WindowProc AfxCallWndProc AfxWndProc So your SendMessage and your menu selection should all be routed through OnCommand first and then to OnCmdMsg. You could put a breakpoint in OnCmdMsg and check on the call stack that OnCmdMsg is called from OnCommand in both instances. Cheers Steen. "To claim that computer games influence children is rediculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

              Y 1 Reply Last reply
              0
              • S Steen Krogsgaard

                as far as I can see all WM_COMMAND messages gets routed to the receiving windows' OnCommand handler, which in turn calls OnCmdMsg (provided some prerequisites are present). Call stack: CCmdTarget::OnCmdMsg CWinApp::OnCmdMsg CWnd::OnCmdMsg CView::OnCmdMsg CFrameWnd::OnCmdMsg CWnd::OnCommand CFrameWnd::OnCommand CWnd::OnWndMsg CWnd::WindowProc AfxCallWndProc AfxWndProc So your SendMessage and your menu selection should all be routed through OnCommand first and then to OnCmdMsg. You could put a breakpoint in OnCmdMsg and check on the call stack that OnCmdMsg is called from OnCommand in both instances. Cheers Steen. "To claim that computer games influence children is rediculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                Y Offline
                Y Offline
                yellowine
                wrote on last edited by
                #7

                Steen Krogsgaard wrote: So your SendMessage and your menu selection should all be routed through OnCommand first and then to OnCmdMsg. You could put a breakpoint in OnCmdMsg and check on the call stack that OnCmdMsg is called from OnCommand in both instances. I did exactly what you described above but I did not see the command got routed to OnCmdMsg. However, it did get routed to OnCommand function. Odd, huh. Is there any difference between menu sent message and message sent by SentMessage or PostMessage?

                S 1 Reply Last reply
                0
                • Y yellowine

                  Steen Krogsgaard wrote: So your SendMessage and your menu selection should all be routed through OnCommand first and then to OnCmdMsg. You could put a breakpoint in OnCmdMsg and check on the call stack that OnCmdMsg is called from OnCommand in both instances. I did exactly what you described above but I did not see the command got routed to OnCmdMsg. However, it did get routed to OnCommand function. Odd, huh. Is there any difference between menu sent message and message sent by SentMessage or PostMessage?

                  S Offline
                  S Offline
                  Steen Krogsgaard
                  wrote on last edited by
                  #8

                  I made a little test program with three menu items: The Option, Post It and Send It. The Option just displays a message box. Post It and Send It posts or sends a WM_COMMAND with IDC_THE_OPTION as wParam to the AfxGetMainWnd() window. I have overriden the view OnCmdMsg and the framewnd OnCmdMsg (it's a SDI app) and put traces in if IDC_THE_OPTION (or any of the other two menu items) passes through. I get what I'd expect: Everything goes through first CFrameWnd::OnCmdMsg and the CView::OnCmdMsg. So there must be something else "wrong" with your app. Cheers Steen. "To claim that computer games influence children is rediculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                  Y 1 Reply Last reply
                  0
                  • S Steen Krogsgaard

                    I made a little test program with three menu items: The Option, Post It and Send It. The Option just displays a message box. Post It and Send It posts or sends a WM_COMMAND with IDC_THE_OPTION as wParam to the AfxGetMainWnd() window. I have overriden the view OnCmdMsg and the framewnd OnCmdMsg (it's a SDI app) and put traces in if IDC_THE_OPTION (or any of the other two menu items) passes through. I get what I'd expect: Everything goes through first CFrameWnd::OnCmdMsg and the CView::OnCmdMsg. So there must be something else "wrong" with your app. Cheers Steen. "To claim that computer games influence children is rediculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                    Y Offline
                    Y Offline
                    yellowine
                    wrote on last edited by
                    #9

                    I appreciated your helped me on this question. I used an MDI app and overriden the CMainFrame's OnCommand. To test if the command message is routed to this function I used an IF statement to see the command ID. I am wondering if you could make a test MDI program on this point, zipped it and sent it to me via email to convince me? Thank you so much!

                    S 1 Reply Last reply
                    0
                    • Y yellowine

                      I appreciated your helped me on this question. I used an MDI app and overriden the CMainFrame's OnCommand. To test if the command message is routed to this function I used an IF statement to see the command ID. I am wondering if you could make a test MDI program on this point, zipped it and sent it to me via email to convince me? Thank you so much!

                      S Offline
                      S Offline
                      Steen Krogsgaard
                      wrote on last edited by
                      #10

                      I tried to mail the MDI program to you but your mailer won't allow a 5 meg zip attach. Anyway, the result is that invoking the command through a menu selection or through SendMessage/PostMessage yield identical call patterns. CMainFrame::OnCommand is called, followed by CChildFrame::OnCommand, which calls CChildFrame::OnCmdMsg and CView::OnCmdMsg. CMainFrame::OnCmdMsg is not called because CChildFrame::OnCmdMsg handles the command (returns TRUE) and thereby shortcuts CMainFrame::OnCommand. One thing that may confuse you is that ON_COMMAND_UI handling is routed through some of the same pathway, namely CMainFrame::OnCmdMsg, CChildFrame::OnCmdMsg and finally CView::OnCmdMsg. Here only the menu selection invocation will cause this chain to be called, as SendMessage/PostMessage does not relate to the UI and hence does not provoke UI updating. So if you just checked whether CMainFrame::OnCmdMsg was called at all you'd see a difference between menu selection and Send-/PostMessage, but that's not for the WM_COMMAND message, thats for MFC's private UI update mechanisms (illustrated by the fact that OnCommand is not called in response to UI updates). This is the best I can do. If you are absolutely sure that WM_COMMAND send by a menu and send by Send-/PostMessage leads to different call stacks I suggest you write a minimum program to reproduce the problem and send it to me or to this forum (oops! I just recommended posting code, sorry!!!) Cheers Steen. "To claim that computer games influence children is rediculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                      Y 1 Reply Last reply
                      0
                      • S Steen Krogsgaard

                        I tried to mail the MDI program to you but your mailer won't allow a 5 meg zip attach. Anyway, the result is that invoking the command through a menu selection or through SendMessage/PostMessage yield identical call patterns. CMainFrame::OnCommand is called, followed by CChildFrame::OnCommand, which calls CChildFrame::OnCmdMsg and CView::OnCmdMsg. CMainFrame::OnCmdMsg is not called because CChildFrame::OnCmdMsg handles the command (returns TRUE) and thereby shortcuts CMainFrame::OnCommand. One thing that may confuse you is that ON_COMMAND_UI handling is routed through some of the same pathway, namely CMainFrame::OnCmdMsg, CChildFrame::OnCmdMsg and finally CView::OnCmdMsg. Here only the menu selection invocation will cause this chain to be called, as SendMessage/PostMessage does not relate to the UI and hence does not provoke UI updating. So if you just checked whether CMainFrame::OnCmdMsg was called at all you'd see a difference between menu selection and Send-/PostMessage, but that's not for the WM_COMMAND message, thats for MFC's private UI update mechanisms (illustrated by the fact that OnCommand is not called in response to UI updates). This is the best I can do. If you are absolutely sure that WM_COMMAND send by a menu and send by Send-/PostMessage leads to different call stacks I suggest you write a minimum program to reproduce the problem and send it to me or to this forum (oops! I just recommended posting code, sorry!!!) Cheers Steen. "To claim that computer games influence children is rediculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                        Y Offline
                        Y Offline
                        yellowine
                        wrote on last edited by
                        #11

                        I am very convinced by your explanation here. Thank you so much! Have a nice weekend.

                        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