MFC消息学习总结

来源:互联网 发布:中文模糊匹配算法 编辑:程序博客网 时间:2024/05/16 06:14
一、

在给类添加消息映射时,首先在类的最后添加DECLARE_MESSAGE_MAP()

这是一个宏,

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

这里的作用是要返回类的消息映射表,即AFX_MSGMAP

AFX_MSGMAP定义如下:

struct AFX_MSGMAP
{
const AFX_MSGMAP* (PASCAL* pfnGetBaseMap)();---->基类的AFX_MSGMAP结构体,用于查找基类的消息映射表
const AFX_MSGMAP_ENTRY* lpEntries;
};

成员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)
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)

};

这个结构体很重要,在类的实现里,会把需要映射消息添加到lpEntries


二、

BEGIN_MESSAGE_MAP(当前类类型,基类类型)---->>这个实际上是实现了GetMessageMap方法,基类类型的作用是当消息在当前类找不到处理函数时,会从基类查找,一级一级往上找。

#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[] =  \

{

=========================

这里就会用ON_WM_XXXX消息填充结构体数组

=========================

#define END_MESSAGE_MAP() \
{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \---->>这个标识了数组的结尾
}; \
static const AFX_MSGMAP messageMap = \
{ &TheBaseClass::GetThisMessageMap, &_messageEntries[0] }; \
return &messageMap; \
}

=================================================================================

分割线

=================================================================================

下面是mfc源文件复制出来的,定义消息可以有9种类型,

我把它分成3类,

1、WM_COMMAND消息,一种是包含nNotifyCode,另一种是CN_COMMAND:

nNotifyCode是使用在控件发送消息时所携带,比如

#define ON_LBN_SELCHANGE(id, memberFxn) \
ON_CONTROL(LBN_SELCHANGE, id, memberFxn)

#define ON_CONTROL(wNotifyCode, id, memberFxn) \
{ WM_COMMAND, (WORD)wNotifyCode, (WORD)id, (WORD)id, AfxSigCmd_v, \
(static_cast< AFX_PMSG > (memberFxn)) },

ON_LBN_SELCHANGE消息最后产生的是WM_COMMAND消息,但它比普通的多了wNotifyCode,普通的则直接

使用CN_COMMAND;

2、WM_NOTIFY

一种新型消息出来方式,能传递更多的信息

3、其他window消息,比如WM_PAINT等等。

// Entries in a message map (a 'AFX_MSGMAP_ENTRY') table can be of 9 formats
//
// 1) control notification message (i.e. in response to WM_COMMAND)
//      WM_COMMAND, nNotifyCode, nControlID, nControlID, signature type, parameterless member function
//      (eg: WM_COMMAND, LBN_SELCHANGE, IDC_LISTBOX, AfxSig_vv, ... )
// 2) control notification message range (i.e. in response to WM_COMMAND)
//      WM_COMMAND, nNotifyCode, nControlIDFirst, nControlIDLast, signature type, parameterless member function
//      (eg: WM_COMMAND, LBN_SELCHANGE, IDC_LISTBOX1, IDC_LISTBOX5, AfxSig_vw, ... )
// 3) WM_NOTIFY notification
//      WM_NOTIFY, nNotifyCode, nControlID, nControlID, signature type, ...)
// 3) Update Command UI
//      -1, 0, nControlID, 0, signature Unknown, parameterless member function
// 4) Update Command UI Range
//      -1, 0, nControlIDFirst, nControlIDLast, signature Unknown, parameterless member function
// 5) menu/accelerator notification message (i.e. special case of first format)
//      WM_COMMAND, 0, nID, 0, signature type, parameterless member function
//      (eg: WM_COMMAND, 0, IDM_FILESAVE, 0, AfxSig_vv, ... )
// 6) menu/accelerator notification message range
//      WM_COMMAND, 0, nIDFirst, nIDLast, signature type, parameterless member function
//      (eg: WM_COMMAND, 0, IDM_FILE_MRU1, IDM_FILE_MRU4, AfxSig_vw, ... )
// 7) constant windows message
//      nMessage, 0, 0, 0, signature type, member function
//      (eg: WM_PAINT, 0, ...)
// 8) variable windows message (using RegisterWindowMessage)
//      0xC000, 0, 0, 0, &nMessage, special member function
//
// The end of the message map is marked with a special value
//      0, 0, AfxSig_end, 0
/////////////////////////////////////////////////////////////////////////////


// Naming scheme:
// <signature> -> AfxSig_<ReturnType>_<WPARAMType>_<LPARAMType>
// <ReturnType> -> b (BOOL)
//                 h (HANDLE)
//                 v (void)
//                 i (int)
//                 l (LRESULT)
// <WPARAMType> ->                  
// Naming scheme:
// b - BOOL
// D - CDC*
// W - CWnd*
// w - UINT
// h - handle
// i - int
// s - LPTSTR
// v - void
// l - LPARAM
// M - CMenu*
// p - CPoint
// POS - WINDOWPOS*
// CALC - NCCALCSIZE_PARAMS*
// NMHDR - NMHDR*
// HELPINFO - HELPINFO*
// SIZING - LPRECT
// cmdui - CCmdUI*
// CDS - COPYDATASTRUCT*
// s - short
// by - byte

原创粉丝点击