I. MFC's message mapping mechanism
1, the use of message mapping mechanism
Class 1.1 must derive from CCmdTarget
Declare macros must be added within class 1.2 Declare_message_map
Implement macro Begin_message_map must be added outside of class 1.3 End_message_map
2. Data structure
struct Afx_msgmap_entry (the type of the macro-expanded static array for each element)
{
UINT nmessage; Message ID
UINT NCode; Notification code
UINT NID; Command ID/control ID.
UINT Nlastid; Last control ID
UINT NSig; Types of Message handler functions
Afx_pmsg PFN; Address of the message handler function (pointer)
};
struct Afx_msgmap (the type of a macro-expanded static variable)
{
Const afx_msgmap* Pbasemap; Save the parent class static variable address (responsible for connecting the linked list)
Const afx_msgmap_entry* Lpentries; The first address of a static array pointing to the corresponding class
};
3, macro expansion of the code
4, the role of the macro expansion of the various parts
_messageentries[]-a static array in which each element of the array holds the message ID and the corresponding handler function
Messagemap-static variable, save the parent class static variable address (responsible for connecting linked list), point to the corresponding class static array first address
Getmessagemap ()-virtual function, get the static variable address of this class (get the node of the chain header)
5. The execution process of message mapping mechanism
5.1 Gets the Frame class object address (Pframe) that is bound together with the window handle
5.2 Use Pframe call Getmessagemap virtual function to get this class static variable address (linked table head node) pmessagemap
5.3 Use Pmessagemap to get a static array of the corresponding class, and match each element in the array, if execution 5.5 is found, if no execution 5.4 is found
5.4 Use Pmessagemap to get the parent class static variable address if NULL traversal ends, if not NULL to execute 5.3
5.5 Use the last member of the found array element and call it to complete the processing of the message
II. Classification of MFC messages
1, Windows standard message (for example: keyboard, mouse, timer ...) )
On_wm_create
On_wm_paint
On_wm_mousemove
...
On_wm_xxxx
All messages beginning with WM_ are standard messages except WM_COMMAND, and classes derived from CWnd can receive such messages;
2. Command Message (WM_COMMAND)
ON_COMMAND (command ID, processing function)
Messages from menus, accelerator keys, toolbar buttons, such messages are presented in wm_command form, different command messages are distinguished by the ID identification of the menu item, and classes derived from CCmdTarget can receive such messages;
3. Notification message (WM_COMMAND)
On_en_change
On_ Notification Code
Messages generated by controls, such as the click of a button, the selection of a list box, and so on, generate such messages in order to notify their parent window (usually a dialog box) of the occurrence of the event, which is also presented in wm_command form; classes derived from CCmdTarget can receive such messages;
4. Custom Messages
#define Wm_mymessage Wm_user+n
On_message
Note: View classes and framework classes can receive both a standard message and a command message and a notification message; The document class and the application class can only receive command messages and notification messages
Third, the processing of the message
1. When the message is generated into the window processing function AfxWndProc
2. Using the Cwnd::fromhandlepermanent function to get the Frame class object address (Pframe) bound together with the window handle
3, using Pframe Call Framework class member virtual function WindowProc completion message processing
Initial knowledge of MFC----message mapping mechanism