CWnd::WindowProc的理解

來源:互聯網
上載者:User

先帖一段原始碼

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最終來設定按鈕在滑鼠移動上去該顯示的狀態。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.