(MFC Application series) menu command routing (Lesson 6), mfc menu command
I. Classification of Windows messages ① except for WM_COMMAND, all messages starting with WM _ are standard messages. Classes derived from the CWnd class can receive such messages. ② The command message comes from the menu, the accelerator key, and the toolbar. Such messages are displayed in the form of WM_COMMAND. In MFC, different life-order messages are distinguished by the menu item ID. In SDK, messages are identified by the wParam parameter. Classes derived from csf-target can receive such messages. ③ A message generated by the control is advertised to notify its parent window of the event. Such messages are also presented in the form of WM_COMMAND. Classes derived from csf-target can receive such messages. The CWnd class is derived from the csf-target class. Therefore, any class derived from CWnd can receive standard messages, command messages, and announcement messages. A class derived from csf-target can only receive command messages and notification messages. 2. In the background, MFC replaces the window procedure function with the AfxWndProc function, which processes all messages. This function calls the AfxCallWndProc function internally. The latter will call the WindowProc function, which is a member function of the CWnd class. All types of messages in the application will enter this function. The WindowProc function will call the OnWndMsg function again, which will judge the type of the incoming message. If it is a standard message, the message ing mechanism is used to find the class that responds to the current message and call the corresponding message ing function to process the message. If it is a command message, it will be handled by the OnCommand function, which will route the command message. If it is a notification message, it will be handled by the OnNotify function, this function completes the route of the advertised message. At last, both of them call the on1_msg function. The specific process of menu command message routing: When you click a menu item, the framework class is the first to receive the menu command message. The framework class sends the received message to its subwindow, that is, the view class, which is first processed by the View class. The class first checks whether the message is responded Based on the Command Message ing mechanism. If the response is received, the corresponding response function is called to process the message, and the message routing process ends; if the View class does not respond to this command message, it is handed over to the document class. The document class also looks for whether it has responded to this menu command. If the response is received, it is processed by the Command Message response function of the document class, and the routing process ends. If the document class does not respond, return the Command Message to the View class and the View class to the framework class. The framework class checks whether it has responded to the command message. If it does not, it submits the menu command message to the application class for processing. Summary: The order of the response menu item commands is as follows: View class, document class, framework class, and application class.