struct AFX_MSGMAP
{
AFX_MSGMAP* pBaseMessageMap;
AFX_MSGMAP_ENTRY* lpEntries;
};
struct AFX_MSGMAP_ENTRY
{
UINT nMessage;
UINT nCode;
UINT nID;
UINT nLastID;
UINT nSig;
AFX_PMSG pfn;
};
typedef void ( CCmdTarget::*AFX_PMSG )( void );
#define DECLARE_MESSAGE_MAP() \
static AFX_MSGMAP_ENTRY _messageEntries[]; \
static AFX_MSGMAP messageMap; \
virtual AFX_MSGMAP* GetMessageMap() const;)
#define BEGIN_MESSAGE_MAP( theClass,baseClass ) \
AFX_MSGMAP* theClass::GetMessageMap() const \
{ return &theClass::messageMap; }
AFX_MSGMAP theClass::messageMap = \
{ &( baseClass::messageMap ),\
(AFX_MSGMAP_ENTRY*)( &theClass::_messageEntries ) }; \
AFX_MSGMAP_ENTRY theClass::_messageEntries[] = \
{
#define ON_COMMAND( id,memberFxn ) \
{ WM_COMMAND,0,(WORD)id,(WORD)id,AfxSig_vv,(AFX_PMSG)memberFxn },
#define END_MESSAGE_MAP() \
{ 0,0,0,0,AfxSig_end,( AFX_PMSG )0 } \
};
简单画个图:
这里只是给出了ON_COMMAND类型的消息,其实在MFC内部,ON_WM_开头的消息,在生成消息映射的时候也是以这种结构,甚至可以说是方法来组装起来的.
可以查看MFC里面的源码:以OnPaint为例:
#define ON_WM_PAINT() \
{ WM_PAINT, 0, 0, 0, AfxSig_vv, \
(AFX_PMSG)(AFX_PMSGW)(void (AFX_MSG_CALL CWnd::*)(void))&OnPaint },
自定义消息也是类似:
#define ON_MESSAGE(message, memberFxn) \
{ message, 0, 0, 0, AfxSig_lwl, \
(AFX_PMSG)(AFX_PMSGW)(LRESULT (AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM))&memberFxn },