What kinds of messages do you write handlers for? There are three main categories:
-
Windows messages
This includes primarily those messages beginning with the WM_ prefix, except for WM_COMMAND . Windows messages are handled by windows and views. These messages often have parameters that are used in determining how to handle the message.
-
Control notifications
This includes WM_COMMAND notification messages from controls and other child windows to their parent windows. For example, an edit control sends its parent a WM_COMMAND message containing the EN_CHANGE control-notification code when the user has taken an action that may have altered text in the edit control. The window's handler for the message responds to the notification message in some appropriate way, such as retrieving the text in the control.
The framework routes control-notification messages like other WM_ messages. One exception, however, is the BN_CLICKED control-notification message sent by buttons when the user clicks them. This message is treated specially as a command message and routed like other commands.
-
Command messages
This includes WM_COMMAND notification messages from user-interface objects: menus, toolbar buttons, and accelerator keys. The framework processes commands differently from other messages, and they can be handled by more kinds of objects, as explained in Command Targets .
Windows Messages and Control-Notification Messages
Messages in categories 1 and 2 — Windows messages and control notifications — are handled by windows: objects of classes derived from class CWnd . This includes CFrameWnd , CMDIFrameWnd , CMDIChildWnd , CView , CDialog , and your own classes derived from these base classes. Such objects encapsulate an HWND , a handle to a Windows window.
Command Messages
Messages in category 3 — commands — can be handled by a wider variety of objects: documents, document templates, and the application object itself in addition to windows and views. When a command directly affects some particular object, it makes sense to have that object handle the command. For example, the Open command on the File menu is logically associated with the application: the application opens a specified document upon receiving the command. So the handler for the Open command is a member function of the application class. For more about commands and how they are routed to objects, see How the Framework Calls a Handler .
所以在CWinApp里面也可以处理第3种方式包括菜单响应。这些都是在CMainFrame里面用OnCmdMsg()这个函数,把所有菜单响应都转发给CWinApp和CView。
如果自己建了个CWnd的子类,想要处理父窗口的消息,就必须在父窗口重载OnCmdMsg()这个函数,并把消息转发给自定义子类,交由子类先处理,如果返回true说明子类已经处理了,一般可以直接return不再由父窗口处理了。