当一个窗口接收到某个消息后,MFC将调用类的成员函数。但是MFC如何知道该调用哪个函数呢?
为了解决上述问题,MFC运用了一个叫做消息映射(Message Map)机制。消息映射就是将消息和所要调用的函数绑定在一起。一旦接受到一个消息,MFC将进入消息映射去寻找与该消息相对应的消息句柄。
MFC采用的是一系列的宏(Macros)去添加消息映射到类中。当运用ClassWizard去添加一个消息句柄时首先添加该 函数到类中然后添加响应的宏到消息映射中。例如若用ClassWizard添加WM_CLOSE的消息句柄时,会有一下三个动作:
1.在类的实现(.cpp)中的消息映射中:

view plaincopy to clipboardprint?BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
2.在类(.h)声明中声明函数:该函数前有afx_msg关键字//{{AFX_MSG_MAP(CAboutDlg)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

view plaincopy to clipboardprint?protected:
//{{AFX_MSG(CAboutDlg)
afx_msg void OnClose();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
protected:
//{{AFX_MSG(CAboutDlg)
afx_msg void OnClose();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
3.在类的实现(.cpp)中://{{AFX_MSG(CAboutDlg)
afx_msg void OnClose();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
protected:
//{{AFX_MSG(CAboutDlg)
afx_msg void OnClose();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()

view plaincopy to clipboardprint?void CAboutDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
CDialog::OnClose();
}
void CAboutDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
CDialog::OnClose();
}
宏DECLARE_MESSAGE_MAP()告知MFC给消息映射添加必要的代码。{
// TODO: Add your message handler code here and/or call default
CDialog::OnClose();
}
void CAboutDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
CDialog::OnClose();
}
宏BEGIN_MESSAGE_MAP()表示消息映射的开始,括弧类的两个参数指示了发送消息的类和其基类。
宏END_MESSAGE_MAP()表示消息映射的结束。
有一些消息ClassWizard并不支持但是可以手工添加。但有得时候没有消息映射宏,这个时候可以采用通用宏ON_MESSAGE,利用该宏可以传递任何消息。
view plaincopy to clipboardprint?afx_msg LRESULT OnMessage(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnMessage(WPARAM wParam, LPARAM lParam);
OnMessage()是句柄函数,ON_MESSAGE有两个参数,句柄的地址和消息。例如下面的例子将WM_GETTEXTLENGTH映射到OnGetTextLength():afx_msg LRESULT OnMessage(WPARAM wParam, LPARAM lParam);
view plaincopy to clipboardprint?ON_MESSAGE (WM_GETTEXTLENGTH, OnGetTextLength)
ON_MESSAGE (WM_GETTEXTLENGTH, OnGetTextLength)
OnGetTextLength的原形为:ON_MESSAGE (WM_GETTEXTLENGTH, OnGetTextLength)
view plaincopy to clipboardprint?afx_msg LRESULT OnGetTextLength(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnGetTextLength(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnGetTextLength(WPARAM wParam, LPARAM lParam);
有得时候,需要在两个窗口或两个应用程序间进行通信,一个简单的解决方法是采用自定义消息。为了避免与系统定义的消息冲突,需要运用一个大于0xBFF的数WM_APP。

view plaincopy to clipboardprint?#define WM_DELETEALL WM_APP + 0x100
//...
pYourDialog->SendMessage(WM_DELETEALL, 0, 0);
#define WM_DELETEALL WM_APP + 0x100
//...
pYourDialog->SendMessage(WM_DELETEALL, 0, 0);
运用宏WM_MESSAGE完成消息映射,将WM_DELETEALL与句柄函数OnDeleteAll()绑定。//...
pYourDialog->SendMessage(WM_DELETEALL, 0, 0);
#define WM_DELETEALL WM_APP + 0x100
//...
pYourDialog->SendMessage(WM_DELETEALL, 0, 0);

view plaincopy to clipboardprint?#define WM_DELETEALL WM_APP + 0x100
//...
//Message Map entry:
ON_MESSAGE (WM_DELETEALL, OnDeleteAll)
//OnDeleteAll is prototyped as
afx_msg LRESULT OnDeleteAll(WPARAM wParam, LPARAM lParam);
//And is implemented as
LRESULT OnDeleteAll(WPARAM wParam, LPARAM lParam){
//Do What ever you want
return somevalue;
}
#define WM_DELETEALL WM_APP + 0x100
//...
//Message Map entry:
ON_MESSAGE (WM_DELETEALL, OnDeleteAll)
//OnDeleteAll is prototyped as
afx_msg LRESULT OnDeleteAll(WPARAM wParam, LPARAM lParam);
//And is implemented as
LRESULT OnDeleteAll(WPARAM wParam, LPARAM lParam){
//Do What ever you want
return somevalue;
}
RegisterWindowMessage()用于定义一个新的窗口消息,必须保证其独一无二。宏ON_REGISTERED_MESSAGE表示该消息。例如://...
//Message Map entry:
ON_MESSAGE (WM_DELETEALL, OnDeleteAll)
//OnDeleteAll is prototyped as
afx_msg LRESULT OnDeleteAll(WPARAM wParam, LPARAM lParam);
//And is implemented as
LRESULT OnDeleteAll(WPARAM wParam, LPARAM lParam){
//Do What ever you want
return somevalue;
}
#define WM_DELETEALL WM_APP + 0x100
//...
//Message Map entry:
ON_MESSAGE (WM_DELETEALL, OnDeleteAll)
//OnDeleteAll is prototyped as
afx_msg LRESULT OnDeleteAll(WPARAM wParam, LPARAM lParam);
//And is implemented as
LRESULT OnDeleteAll(WPARAM wParam, LPARAM lParam){
//Do What ever you want
return somevalue;
}

view plaincopy to clipboardprint?class CMyWnd : public CMyParentWndClass
{
public:
CMyWnd();
//{{AFX_MSG(CMyWnd)
afx_msg LRESULT OnFind(WPARAM wParam, LPARAM lParam);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
static UINT WM_FIND = RegisterWindowMessage("YOURAPP_FIND_MSG");
BEGIN_MESSAGE_MAP(CMyWnd, CMyParentWndClass)
//{{AFX_MSG_MAP(CMyWnd)
ON_REGISTERED_MESSAGE(WM_FIND, OnFind)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
class CMyWnd : public CMyParentWndClass
{
public:
CMyWnd();
//{{AFX_MSG(CMyWnd)
afx_msg LRESULT OnFind(WPARAM wParam, LPARAM lParam);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
static UINT WM_FIND = RegisterWindowMessage("YOURAPP_FIND_MSG");
BEGIN_MESSAGE_MAP(CMyWnd, CMyParentWndClass)
//{{AFX_MSG_MAP(CMyWnd)
ON_REGISTERED_MESSAGE(WM_FIND, OnFind)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
用该方式定义的消息的范围为0xC000 - 0xFFFF,并采用sendmessage()发送该消息。{
public:
CMyWnd();
//{{AFX_MSG(CMyWnd)
afx_msg LRESULT OnFind(WPARAM wParam, LPARAM lParam);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
static UINT WM_FIND = RegisterWindowMessage("YOURAPP_FIND_MSG");
BEGIN_MESSAGE_MAP(CMyWnd, CMyParentWndClass)
//{{AFX_MSG_MAP(CMyWnd)
ON_REGISTERED_MESSAGE(WM_FIND, OnFind)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
class CMyWnd : public CMyParentWndClass
{
public:
CMyWnd();
//{{AFX_MSG(CMyWnd)
afx_msg LRESULT OnFind(WPARAM wParam, LPARAM lParam);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
static UINT WM_FIND = RegisterWindowMessage("YOURAPP_FIND_MSG");
BEGIN_MESSAGE_MAP(CMyWnd, CMyParentWndClass)
//{{AFX_MSG_MAP(CMyWnd)
ON_REGISTERED_MESSAGE(WM_FIND, OnFind)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
view plaincopy to clipboardprint?static UINT WM_FIND = RegisterWindowMessage("YOURAPP_FIND_MSG");
//...
pFindWindow->SendMessage(WM_FIND, lParam, wParam);
//...
pFindWindow->SendMessage(WM_FIND, lParam, wParam);
[ 本帖最后由 hlmzrdongdong 于 2011-8-22 23:11 编辑 ]