风魂源码之消息的实现

 

MFC定义DECLARE_MESSAGE_MAP如下:

typedef void (CCmdTarget::*AFX_PMSG)(void);  // CCmdTarget类成员函数指针(注意:参数为void,返回值为void)

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 #  消息的参数和返回值的类型(枚举类型)

  AFX_PMSG pfn;    // routine to call (or special value)  

};
struct AFX_MSGMAP{ 

   const AFX_MSGMAP* (PASCAL* pfnGetBaseMap)();  // 函数指针,返回值为基类消息映射结构体

  const AFX_MSGMAP_ENTRY* lpEntries;        // 子类消息映射入口

};

 

#define DECLARE_MESSAGE_MAP()  \

protected:  \

  static const AFX_MSGMAP* PASCAL GetThisMessageMap(); \

  virtual const AFX_MSGMAP* GetMessageMap() const; \

 

#define BEGIN_MESSAGE_MAP(theClass, baseClass)  \

 PTM_WARNING_DISABLE  \

const AFX_MSGMAP* theClass::GetMessageMap() const  \

{ 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

 

windsoul对于消息的实现采用模仿MFC的方式:

typedef int (WWindow::*WWndFunc)(DWORD,DWORD);  // 就只有两个参数的函数指针,与MFC相比简单了许多 
struct WMessageFunc { DWORD id; WWndFunc func;};   // 结构体也只有两个元素,消息Id和处理该消息的函数指针地址
#define MAP_MESSAGE_FUNC()  \

public:  \

  virtual const WMessageFunc* GetMessageEntries() { return messageEntries; }  \

  static const WMessageFunc messageEntries[];

 

#define BEGIN_MAP_MESSAGE(thisclass,baseclass)  \

const WMessageFunc thisclass::messageEntries[]={  \

{(DWORD)(baseclass::messageEntries),0},

 

#define MAP_MESSAGE(id,func) {id,(WWndFunc)&func},  

#define END_MAP_MESSAGE() {0,0}};

posted @ 2010-11-15 11:17    阅读(374)  评论(0)    收藏  举报