MFC on the structure of the message map for the encapsulated message AFX_MSGMAP_ENTRYstruct AFX_MSGMAP_ENTRY {UINT nMessage; // windows message UINT nCode; // control code or WM_NOTIFY code UINT nID; // control ID (or 0 for windows messages) UINT nLastID; // used for entries specifying a range of control id's UINT_PTR nSig; // signature type (action) or pointer to message # AFX_PMSG pfn; // routine to call (or special value)}; wherein typedef void (AFX_MSG_CALL CCmdTarget :: * AFX_PMSG) (void); structure constituting the subject map message AFX_MSGMAPstruct AFX_MSGMAP {const AFX_MSGMAP * pBaseMap; const AFX_MSGMAP_ENTRY * lpEntries;}; in this way it would build a key figure objects AFX_MSGMAP message mapping table, it is a hand holding base class AFX_MSGMAP object, the other hand draws a message mapping table of the class itself, so that only the AFX_MSGMAP object is placed correctly, the entire message mapping table is established. So what is correct? The meaning is 2: First, the correct settings of PBASEMAP, telling it to the base class, and the other is the correct establishment of class itself message mapping table. These two work are done by 4 macros, they are: declare_memssage_map () / begin_message_map () / on_command () (on_command macro is only to handle the command message, there is also a corresponding macro for other messages, but the principle is the same ) / Message_map ()
#define DECLARE_MESSAGE_MAP () / private: / static const AFX_MSGMAP_ENTRY _messageEntries []; / protected: / static const AFX_MSGMAP messageMap; / virtual const AFX_MSGMAP * GetMessageMap () const; / macro this effect are 3: Insert a static member within the class _MessageEnTries, this is an array of messages to store the class to process (ie, the message mapping table of the class itself), another static member MassageMap is used to point to the message mapping table of the base class, and the content is to be implemented.
Next, _MessageEntries initialization, the correct pointing of MessageMap, the implementation of the GetMessageMap function is not done, it is the responsibility of the last three macros, they want to use, and work is normal.
#define BEGIN_MESSAGE_MAP (theClass, baseClass) / const AFX_MSGMAP * theClass :: GetMessageMap () const / {return & theClass :: messageMap;} / AFX_COMDAT const AFX_MSGMAP theClass :: messageMap = / {& baseClass :: messageMap, & theClass :: _ messageEntries [0 ]}; / Afx_comdat const AFX_MSGMAP_ENTRY THECLASS :: _ MessageEntries [] = / {/ this macro's role is 3: Define the virtual function getMessageMap () inserted in the class, just simply returning the address of the MessageMap object initialization MessageMap, derived Class and base classes Connect a large message mapping table for the initialization of the message mapping table of the class itself Make a grammatism, the role of the macro is to add the command message to the _MessageEntries array, in fact, in the MFC There are many more convenient macros to add messages to the class, such as om_wm_paint, etc., here, we mainly discuss ON_COMMAND, after all, the principle is the same. #define on_command (id, memberfxn) / {wm_command, cn_command, (word) id, (word) id, afxsigcmd_v, / static_cast
Finally, when all of the information is added, use the End_Message_Map () macro notification MFC a class message mapping table end. #define end_MESSAGE_MAP () / {0, 0, 0, 0, AFXSIG_END, (AFX_PMSG) 0} /}; / Implementation method is simple, nothing more than a full 0 AFX_MESSAGE_MAP object.