CCmdTarget类

CCmdTarget类封装MFC消息映射机制,所以希望接受系统事件和窗口消息的类使用消息映射必须继承自CCmdTarget类,比如CWnd和CDocument分支。

CCmdTarget的虚函数OnCmdMsg用来默认实现传递和发送消息,更新用户界面对象状态。

 

 

 

 

添加消息映射:

 

 

 

各个定义的具体实现:

#define DECLARE_MESSAGE_MAP() \
protected: \
    static const AFX_MSGMAP* PASCAL GetThisMessageMap(); \
    virtual const AFX_MSGMAP* GetMessageMap() const; \

 

struct AFX_MSGMAP
{
    const AFX_MSGMAP* (PASCAL* pfnGetBaseMap)();  //指向父类方法的指针
    const AFX_MSGMAP_ENTRY* lpEntries;          //路由表指针,消息映射数组首地址,即本类的静态数组首地址
};
struct AFX_MSGMAP_ENTRY
{
    UINT nMessage;   // windows message消息
    UINT nCode;      // control code or WM_NOTIFY code
    UINT nID;        // control ID (or 0 for windows messages)控件ID
    UINT nLastID;    // used for entries specifying a range of control id's控件ID范围
    UINT_PTR nSig;       // signature type (action) or pointer to message #函数类型
    AFX_PMSG pfn;    // routine to call (or special value)   //回调函数
};
#define BEGIN_MESSAGE_MAP(theClass, baseClass) \
    PTM_WARNING_DISABLE \
    const AFX_MSGMAP* theClass::GetMessageMap() const \   //GetMessageMap()虚函数,返回AFX_MSGMAP*
        { return GetThisMessageMap(); } \
    const AFX_MSGMAP* PASCAL theClass::GetThisMessageMap() \
    { \
        typedef theClass ThisClass;                           \
        typedef baseClass TheBaseClass;                       \
        static const AFX_MSGMAP_ENTRY _messageEntries[] =  \
        {

 

#define END_MESSAGE_MAP() \
        {0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \
    }; \
        static const AFX_MSGMAP messageMap = \
        { &TheBaseClass::GetThisMessageMap, &_messageEntries[0] }; \
        return &messageMap; \
    }                                  \
    PTM_WARNING_RESTORE
#define ON_WM_CREATE() \
    { WM_CREATE, 0, 0, 0, AfxSig_is, \
        (AFX_PMSG) (AFX_PMSGW) \
        (static_cast< int (AFX_MSG_CALL CWnd::*)(LPCREATESTRUCT) > ( &ThisClass :: OnCreate)) },

 

 

 

 

 

 

 

 

posted @ 2020-03-30 09:52  坦坦荡荡  阅读(951)  评论(0)    收藏  举报