MFC消息机制以及其原理简单介绍

类定义

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这个类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值