类定义
struct AFX_MSGMAP
{
constAFX_MSGMAP* (PASCAL*pfnGetBaseMap)();//指向基类的指针
constAFX_MSGMAP_ENTRY* lpEntries;//指向一个数组
};
//定义一个函数指针
typedef void (AFX_MSG_CALL CCmdTarget::*AFX_PMSG)(void);
struct AFX_MSGMAP_ENTRY
{
UINTnMessage; // windows message
UINTnCode; // control code or WM_NOTIFY code
UINTnID; // control ID (or 0 for windows messages)
UINTnLastID; // used for entries specifying a range ofcontrol id's
UINT_PTRnSig; // signature type (action) or pointer to message#
AFX_PMSGpfn; // routine to call (or special value)
};
宏
DECLARE_MESSAGE_MAP
static const AFX_MSGMAP* PASCALGetThisMessageMap(); \
virtual const AFX_MSGMAP* GetMessageMap()const; \
宏BEGIN_MESSAGE_MAP和END_MESSAGE_MAP
把theClass替换为CView
BEGIN_MESSAGE_MAP(theClass, baseClass) \
PTM_WARNING_DISABLE \
constAFX_MSGMAP* CView::GetMessageMap()const \
{return GetThisMessageMap();} \
constAFX_MSGMAP* PASCALCView::GetThisMessageMap() \
{ \
typedefCView ThisClass; \
typedefCWnd TheBaseClass; \
staticconst AFX_MSGMAP_ENTRY_messageEntries[] = \
{
在这里写入消息列表(这里的消息列表后面有讲解)
如 ON_TIMER
ON_NOTIFY
ON_COMMAND等
#define END_MESSAGE_MAP() \
{0,0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \
}; \
staticconst AFX_MSGMAPmessageMap = \
{ &TheBaseClass::GetThisMessageMap,&_messageEntries[0] }; \
return&messageMap; \
} \
消息列表
可以把消息分为三类
窗口消息,一般是ON_WM_XX
通告消息,一般是ON_NOTIFY
命令消息,一般是ON_COMMAND或者ON_BN_CLICKED
这里需要注意的是我们在BEGIN_MESSAGE_MAP()和END_MESSAGE_MAP之间的消息
都会转为统一格式(也就是创建一个AFX_MSGMAP_ENTRY结构体)
如:
窗口消息
#define ON_WM_PAINT() \
{ WM_PAINT,0, 0, 0, AfxSig_vv, \
(AFX_PMSG)(AFX_PMSGW) \
(static_cast<void (AFX_MSG_CALLCWnd::*)(void)> ( &ThisClass :: OnPaint)) },
通告消息
#define ON_NOTIFY(wNotifyCode, id,memberFxn) \
{ WM_NOTIFY,(WORD)(int)wNotifyCode, (WORD)id, (WORD)id, AfxSigNotify_v,\
(AFX_PMSG)\
(static_cast<void (AFX_MSG_CALLCCmdTarget::*)(NMHDR*,LRESULT*) > \
(memberFxn))},
命令消息
#define ON_BN_CLICKED(id, memberFxn)\
ON_CONTROL(BN_CLICKED, id,memberFxn)
// for general controls
#define ON_CONTROL(wNotifyCode, id,memberFxn) \
{ WM_COMMAND,(WORD)wNotifyCode,(WORD)id,(WORD)id,AfxSigCmd_v, \
(static_cast<AFX_PMSG > (memberFxn))},
注意最后一个参数所有的消息都要转为AFX_PMSG这个类型