先帖一段原始碼
LRESULT CWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam){// OnWndMsg does most of the work, except for DefWindowProc callLRESULT lResult = 0;if (!OnWndMsg(message, wParam, lParam, &lResult))lResult = DefWindowProc(message, wParam, lParam);return lResult;}
建立一對話方塊程式CTestDlg,在其中添加 class CMyButton : public CButton{};
#include "stdafx.h"#include "TestCom.h"#include "MyButton.h"// CMyButtonIMPLEMENT_DYNAMIC(CMyButton, CButton)CMyButton::CMyButton(){}CMyButton::~CMyButton(){}BEGIN_MESSAGE_MAP(CMyButton, CButton)ON_WM_MOUSEMOVE()END_MESSAGE_MAP()// CMyButton message handlersvoid CMyButton::OnMouseMove(UINT nFlags, CPoint point){// TODO: Add your message handler code here and/or call defaultTRACE(L"CMyButton::OnMouseMove\n");CButton::OnMouseMove(nFlags, point);}BOOL CMyButton::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult){// TODO: Add your specialized code here and/or call the base classif (message == WM_MOUSEMOVE) {int a = 20;}return CButton::OnWndMsg(message, wParam, lParam, pResult);}
此時,因為你重載了OnMouseMove,修改了訊息映射地圖,那麼在CMyButton接收MouseMove訊息的時候,會映射到
CMyButton::OnMouseMove,但是CMyButton::OnMouseMove由CButton::OnMouseMove預設處理。然後CWnd::OnWndMsg
直接返回TRUE;那麼CWnd::WndProc下面的
lResult = DefWindowProc(message, wParam, lParam);
便不會再執行了
反之,倘若你沒重載WM_MOUSEMOVE訊息(只要注釋ON_WM_MOUSEMOVE()即可)。那麼OnWndMsg會返回FALSE,表明使用者
沒有修改訊息映射,然後
lResult = DefWindowProc(message, wParam, lParam);
會被執行,設定按鈕狀態。
但是請注意:CButton::OnMouseMove最終還是會調用CWnd::DefWindowProc這個函數,這個函數的實現如下:
LRESULT CWnd::DefWindowProc(UINT nMsg, WPARAM wParam, LPARAM lParam){if (m_pfnSuper != NULL)return ::CallWindowProc(m_pfnSuper, m_hWnd, nMsg, wParam, lParam);WNDPROC pfnWndProc;if ((pfnWndProc = *GetSuperWndProcAddr()) == NULL)return ::DefWindowProc(m_hWnd, nMsg, wParam, lParam);elsereturn ::CallWindowProc(pfnWndProc, m_hWnd, nMsg, wParam, lParam);}
看到沒,::CallWindowProc(m_pfnSuper, m_hWnd, nMsg, wParam, lParam);
這個API最終來設定按鈕在滑鼠移動上去該顯示的狀態。