我们以CListCtrl控件为例来分析WM_NOTIFY消息。 CListCtrl控件在Report样式下会包含CHeaderCtrl标头控件,即CHeaderCtrl标头控件为CListCtrl控件的子控件,所以不难理解,拖动CHeaderCtrl标头控件的列分隔栏会投递HDN_BEGINTRACK消息(WM_NOTIFY消息,通知码为HDN_BEGINTRACK)给其父窗口CListCtrl控件。
但是,我们在对话框窗口中也可以收到CHeaderCtrl标头控件的HDN_BEGINTRACK消息,这作何解释呢?
如下所示:我们在对话框窗口中响应HDN_BEGINTRACK消息,当开始拖动标头控件时,弹出消息框提示。
- BEGIN_MESSAGE_MAP(CTestDlgDlg, CDialog)
- ......
- ON_NOTIFY(HDN_BEGINTRACK, 0, &CTestDlgDlg::OnHdnBegintrackList1)
- ......
- END_MESSAGE_MAP()
- void CTestDlgDlg::OnHdnBegintrackList1(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
- // TODO: Add your control notification handler code here
- AfxMessageBox(TEXT("CHeaderCtrl HDN_BEGINTRACK消息!"));
- *pResult = 0;
- }
- </LPNMHEADER>
BEGIN_MESSAGE_MAP(CTestDlgDlg, CDialog) ...... ON_NOTIFY(HDN_BEGINTRACK, 0, &CTestDlgDlg::OnHdnBegintrackList1) ...... END_MESSAGE_MAP() void CTestDlgDlg::OnHdnBegintrackList1(NMHDR *pNMHDR, LRESULT *pResult) { LPNMHEADER phdr = reinterpret_cast(pNMHDR); // TODO: Add your control notification handler code here AfxMessageBox(TEXT("CHeaderCtrl HDN_BEGINTRACK消息!")); *pResult = 0; }
效果如下:
在文章MFC消息处理流程概述中可知,CListCtrl控件窗口在接收到HDN_BEGINTRACK消息时,会调用以下代码处理:
- LRESULT CWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- // OnWndMsg does most of the work, except for DefWindowProc call
- LRESULT lResult = 0;
- if (!OnWndMsg(message, wParam, lParam, &lResult))
- lResult = DefWindowProc(message, wParam, lParam);
- return lResult;
- }
LRESULT CWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { // OnWndMsg does most of the work, except for DefWindowProc call LRESULT lResult = 0; if (!OnWndMsg(message, wParam, lParam, &lResult)) lResult = DefWindowProc(message, wParam, lParam); return lResult; }
很显然,HDN_BEGINTRACK消息在CListCtrl的消息映射表中没有对应的消息处理函数处理,在CHeaderCtrl的消息映射表也无对应的反射消息处理函数处理,所以,以上代码OnWndMsg(message, wParam, lParam, &lResult)会返回FALSE,所以会交由默认的消息处理函数DefWindowProc(message, wParam, lParam)处理,该函数会将该消息投递给消息对应窗口(message.hwnd)的父窗口处理,即对话框窗口(当然,对应的message.hwnd变为了对话框窗口),这样对话框窗口才有机会处理该消息而弹出消息框。
如果在CListCtrl中处理了HDN_BEGINTRACK消息,则就不会交由对话框窗口处理。
为此,我们增加CMyListCtrl,继承自CListCtrl,在CMyListCtrl中处理HDN_BEGINTRACK消息。
- BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
- ON_NOTIFY(HDN_BEGINTRACKA, 0, &CMyListCtrl::OnHdnBegintrack)
- ON_NOTIFY(HDN_BEGINTRACKW, 0, &CMyListCtrl::OnHdnBegintrack)
- END_MESSAGE_MAP()
- void CMyListCtrl::OnHdnBegintrack(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
- // TODO: Add your control notification handler code here
- AfxMessageBox(TEXT("CMyListCtrl处理了HDN_BEGINTRACK消息"));
- *pResult = 0;
- }
- </LPNMHEADER>
BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl) ON_NOTIFY(HDN_BEGINTRACKA, 0, &CMyListCtrl::OnHdnBegintrack) ON_NOTIFY(HDN_BEGINTRACKW, 0, &CMyListCtrl::OnHdnBegintrack) END_MESSAGE_MAP() void CMyListCtrl::OnHdnBegintrack(NMHDR *pNMHDR, LRESULT *pResult) { LPNMHEADER phdr = reinterpret_cast(pNMHDR); // TODO: Add your control notification handler code here AfxMessageBox(TEXT("CMyListCtrl处理了HDN_BEGINTRACK消息")); *pResult = 0; }
同时将对话框中的CListCtrl m_list改为CMyListCtrl m_list。则效果如下:
实例代码:http://download.csdn.net/detail/wangyao1052/4633804
from:http://blog.csdn.net/wangyao1052/article/details/8057828