OnCmdMsg Question
-
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 usingPostMessage(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? -
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 usingPostMessage(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?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
-
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
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.
-
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.
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
-
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
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.
-
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.
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"
-
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"
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?
-
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?
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"
-
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"
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!
-
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!
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"
-
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"