解剖MFC自动生成的宏定义

本文详细解析了MFC在自动生成的对话框项目中使用的DECLARE_MESSAGE_MAP宏定义,包括ON_WM_PAINT()、BEGIN_MESSAGE_MAP()、END_MESSAGE_MAP()等的实现细节。通过对AFX_MSGMAP及其相关结构体的分析,揭示了MFC消息映射的工作原理。
摘要由CSDN通过智能技术生成

一、关于DECLARE_MESSAGE_MAP宏定义
使用MFC向导,在ApplicationType页面选择DialogBased,生成一个对话框项目,Dialog类命名为CCapturePacketDlg,在CCapturePacketDlg.cpp中自动产生下列代码:

1 BEGIN_MESSAGE_MAP(CCapturePacketDlg, CDialog)
2     ON_WM_PAINT()
3 END_MESSAGE_MAP()
  1. 先来分析ON_WM_PAINT(),在头文件“afxmsg.h”有它的宏定义,如下:
1 #define  ON_WM_PAINT() /
2      { WM_PAINT,  0 0 0 , AfxSig_vv, /
3         (AFX_PMSG)(AFX_PMSGW) /
4         (static_cast <   void  (AFX_MSG_CALL CWnd:: * )( void >  (  & ThisClass :: OnPaint)) }
,
说明:层次序号x.y.z表示x为根节点也就是上面代码中的行号,y、z为上一级的定义展开。
2.1 #define WM_PAINT                        0x000F
2.2 AfxSig_vv = AfxSig_v_v_v
2.2.1 enum AfxSig::AfxSig_v_v_v = 19

3.1 AFX_PMSG:typedef void (AFX_MSG_CALL CCmdTarget::*AFX_PMSG)(void); //为一个函数指针
3.2 AFX_PMSGW:typedef void (AFX_MSG_CALL CWnd::*AFX_PMSGW)(void);   //为一个函数指针

将ON_WM_PAINT()完全展开:
1 {
2        0x000F
3        0,
4        0,
5        0,
6        19,
7        //Converts OnPaint to the type of CCmdTarget finally. Derive Class 's pointer -> Base Class's pointer
8        (AFX_MSG_CALL CCmdTarget::*)((AFX_MSG_CALL CWnd::*)(static_cast< void (AFX_MSG_CALL CWnd::*)(void>(&ThisClass :: OnPaint))
9    }

   2.   再来分析BEGIN_MESSAGE_MAP(CCapturePacketDlg, CDialog),在“afxwin.h”中有定义:

 1 #define  BEGIN_MESSAGE_MAP(theClass, baseClass) /
 2     PTM_WARNING_DISABLE /
 3      const  AFX_MSGMAP *  theClass::GetMessageMap()  const  /
 4          return GetThisMessageMap(); }  /
 5      const  AFX_MSGMAP *  PASCAL theClass::GetThisMessageMap() /
 6      { /
 7        typedef theClass ThisClass;                           /
 8        typedef baseClass TheBaseClass;                       /
 9        static const AFX_MSGMAP_ENTRY _messageEntries[] =  /
10        {

2.1 PTM_WARNING_DISABLE:
#define PTM_WARNING_DISABLE /
 __pragma(warning( push ))  / //#pragma warning( push [ ,n ] ),Where n represents a warning level (1 through 4).
                                              //The pragma warning( push ) stores the current warning state for all warnings.
 __pragma(warning( disable : 4867 ))//Do not issue the specified warning message(s).
//http://msdn2.microsoft.com/en-us/2c8f766e.aspx
// Allows selective modification of the behavior of compiler warning messages.
3.1 struct AFX_MSGMAP
 {
  3.1.1 const AFX_MSGMAP* (PASCAL* pfnGetBaseMap)();
  3.1.2 const AFX_MSGMAP_ENTRY* lpEntries;
 };
3.1.2 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)
  UINT nLastID;    // used for entries specifying a range of control id's
  UINT_PTR nSig;       // signature type (action) or pointer to message #
  3.1.2.1 AFX_PMSG pfn;    // routine to call (or special value)
 };
 3.1.2.1 typedef void (AFX_MSG_CALL CCmdTarget::*AFX_PMSG)(void);

5.1 #define PASCAL      __stdcall
将BEGIN_MESSAGE_MAP(CCapturePacketDlg, CDialog)完全展开:

 1 __pragma(warning( push )) __pragma(warning( disable :  4867  ))
 2      const   struct  AFX_MSGMAP *  CCapturePacketDlg::GetMessageMap()  const
 3     
 4        return GetThisMessageMap(); 
 5    }

 6      const   struct  AFX_MSGMAP *  __stdcall CCapturePacketDlg::GetThisMessageMap()
 7      {
 8        typedef CCapturePacketDlg ThisClass;                           
 9        typedef CDialog TheBaseClass;        
10        static const struct AFX_MSGMAP_ENTRY _messageEntries[] = 
11        {

   3    最后分析END_MESSAGE_MAP(),在“afxwin.h”中有定义:

1 #define  END_MESSAGE_MAP() /
2          { 0000, AfxSig_end, (AFX_PMSG)0 }  /
3     }; /
4          static   const  AFX_MSGMAP messageMap  =  /
5          &TheBaseClass::GetThisMessageMap, &_messageEntries[0] } ; /
6          return   & messageMap; /
7     }                                  /
8     PTM_WARNING_RESTORE

2.1 AfxSig_end:enum AfxSig.AfxSig_end = 0
2.2 AFX_PMSG:typedef void (AFX_MSG_CALL CCmdTarget::*AFX_PMSG)(void);//函数指针

4.1 struct AFX_MSGMAP
 {
  const AFX_MSGMAP* (PASCAL* pfnGetBaseMap)();
  const AFX_MSGMAP_ENTRY* lpEntries;
 };

8.1 #define PTM_WARNING_RESTORE /
 __pragma(warning( pop ))
//pop restores the state of all warnings (including 4705, 4706, and 4707) to what it was at the beginning of the code.

·最后将

1 BEGIN_MESSAGE_MAP(CCapturePacketDlg, CDialog)
2     ON_WM_PAINT()
3 END_MESSAGE_MAP()
完全展开为:
 1 __pragma(warning( push )) __pragma(warning( disable :  4867  ))
 2      const   struct  AFX_MSGMAP *  CCapturePacketDlg::GetMessageMap()  const
 3     
 4        return GetThisMessageMap(); 
 5    }

 6      const   struct  AFX_MSGMAP *  __stdcall CCapturePacketDlg::GetThisMessageMap()
 7      {
 8        typedef CCapturePacketDlg ThisClass;                           
 9        typedef CDialog TheBaseClass;        
10        static const struct AFX_MSGMAP_ENTRY _messageEntries[] = 
11        {
12            
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值