Classification of MFC messages in Visual C++

  • 2020-04-01 21:25:39
  • OfStack

Standard (window) messages: window messages are typically related to the inner workings of a window, such as creating a window, drawing a window, and destroying a window. Typically, messages are sent from a system to a window, or from a window to a system. Send the function SendMessage() or PostMessage(). All messages starting with WM_ except WM_COMMAND. Classes derived from CWnd can receive such messages. Note: the standard message does not require us to specify the handler name, it is the default correspondence.

The macro name corresponds to the message handler
ON_WM_CHAR WM_CHAR OnChar
ON_WM_CLOSE WM_CLOSE OnClose
ON_WM_CREATE WM_CREATE message handler OnCreate
ON_WM_DESTROY WM_DESTROY OnDestroy
ON_WM_LBUTTONDO WNWM_LBUTTONDOWN OnLButtonDown
ON_WM_LBUTTONUP WM_LBUTTONUP OnLButtonUp
ON_WM_MOUSEMOVE WM_MOUSEMOVE OnMouseMove
ON_WM_PAINTWM_PAINT OnPaint

Command messages: command messages are usually associated with processing user requests and are generated when the user clicks on a menu or toolbar. The class or function that is sent to process the message (such as load text, save options, etc.) is rendered as WM_COMMAND. In MFC, different command messages are distinguished by menu identification (ID). In the SDK, it is identified by the wParam parameter of the message. A class derived from CCmdTarget can receive such a message, and its wParam records which menu item the message came from.

ON_COMMAND (IDM_ABOUT OnAbout)
ON_COMMAND (IDM_FILENEW OnFileNew)
ON_COMMAND (IDM_FILEOPEN OnFileOpen)
ON_COMMAND (IDM_FILESAVE OnFileSave)

Control message: a message generated by a control, such as a click of a button, a selection of a list box, etc., to inform its parent window (usually a dialog box) of an event. This type of message is also presented as WM_COMMAND. Classes derived from CCmdTarget can receive such messages.

Control macro message handler
The Button ON_BN_CLICKED (< Id> , < MemberFxn>) memberFxn
ComboBox ON_CBN_DBLCLK (< Id> , < MemberFxn>) memberFxn
Edit ON_EN_SETFOCUS (< Id> , < MemberFxn>) memberFxn
ListBox ON_LBN_DBLCLK (< Id> , < MemberFxn>) memberFxn

CWnd is a derived class of CCmdTarget. CMenuApp and CMenuDoc in MFC's single document program are both derived classes of CCmdTarget and do not contain CWnd class.

MFC command message routing: AfxWndProc (replace window procedure function) -> AfxCallWndProc - > WindowProc - > OnWnddMsg - > (if it is a command message, call Oncommand; OnNotify is called if it is an announcement message. OnCmdMsg
Distinction between standard and non-standard messages: standard messages: with subsequent operations after control; Non-standard message: just a simple reminder.

You can design "message image tables" in many categories of your program (each category can only have one message image table, but you can have none) to receive and process messages. You can block any Windows message as long as it is a CWnd derivative. Window-independent MFC categories (such as CDocument and CWinApp) must be derived from CCmdTarget if they also want to process messages, and only WM_COMMAND messages can be received.

Related articles: