Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
msdn says that CCmdTarget is the base class for MFC's message-map architecute. also we know that in MFC there are macros such as DECLARE_MESSAGE_MAP, BEGIN_MESSAGE_MAP, END_MESSAGE_MAP etc to handle the message mapping. these macros provides the implementaion of GetMessageMap() which seraches for the handler function of each messages satrting from main window to its base class. It sounds like macros do everything needed for message mapping. Then where comes the role of CCmdTarget class and how it helps in message mapping?
Could anyone help in understanding this concept?
Posted
Updated 30-May-11 20:33pm
v2

Windows sends messages that are not command messages directly to a window whose handler for the message is then called. However, the framework routes commands to a number of candidate objects — called "command targets" — one of which normally invokes a handler for the command. The handler functions work the same way for both commands and standard Windows messages
If we insert some new object into the routing, its class must be a command-target class. In we are overriding versions of OnCmdMsg, we call the version that we're overriding.

Here is the virtual function defined in CCmdTarget class
// route and dispatch standard command message types
virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra,AFX_CMDHANDLERINFO* pHandlerInfo);


Here AFX_CMDHANDLERINFO contains info about where the command is handled
 
Share this answer
 
Directly or Indirectly classes is derived from CCmdTarget.
CWnd, CMainFrame, CWinApp, CDocument are all derived from CCmdTarget. CCmdTarget is giving a message mapping functionality in MFC.
Whenever an events occur like pressing a key, mouse click with regards to our application. Message loop is responsible to keep processing any message and sent to the application.
GetMessage() collects the message sent to the application by OS. DispatchMessage() function post the message in a message queue.
Winproc() search message in an array of message map and call appropriate handler by using array entry and processed message.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900