簡體   English   中英

對CWnd :: OnLButtonDown()和CTreeCtrl :: OnLButtonDown()感到困惑

[英]Confused about CWnd::OnLButtonDown() and CTreeCtrl::OnLButtonDown()

MFC庫參考
CWnd :: OnLButtonDown

void CMyCla::OnLButtonDown(UINT nFlags, CPoint point)
{
    CWnd::OnLButtonDown(nFlags, point);
}


void CMyTreeCla::OnLButtonDown(UINT nFlags, CPoint point)
{
    CTreeCtrl::OnLButtonDown(nFlags, point);
}

我知道繼承。

class CTreeCtrl : public CWnd
{
......
}

我要調用OnLButtonDown()時是否要遵循任何明確的規則?

謝謝。

如果要先調用父類實現,則可以調用父類的OnLButtonDown(),然后添加實現。

這就是你想要的。

在類頭中,您需要聲明消息映射,並編寫函數頭。

Class myCWnd : public CWnd
{
    DECLARE_MESSAGE_MAP() //note, no semi colon

    afx_msg void OnLButtonDown( UINT nFlags, CPoint pt );
};

在cpp文件中:

BEGIN_MESSAGE_MAP(myCWnd, CWnd)
    ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

void myCWnd::OnLButtonDown( UINT nFlags, CPoint pt )
{
    //do what you want here

    CWnd::OnLButtonDown(nFlags, pt); //call base class method
}

通常,您可以在實現中對事件進行操作,然后調用父類的實現。 這段代碼大師在本教程的第2步中顯示了一個很好的示例。 但這取決於您到底想對OnLButtonDown事件執行什么操作,因此在您的情況下可能是相反的情況。

我假設您的示例中的繼承如下:

class CMyCla : public CWnd
{
}

class CMyTreeCla : public CTreeCtrl
{
......
}

因此,的確,您可以在OnLButtonDown中執行您的操作,然后調用父實現:

void CMyCla::OnLButtonDown(UINT nFlags, CPoint point)
{
    // Your stuff here
    // blah
    // end your stuff
    CWnd::OnLButtonDown(nFlags, point);
}


void CMyTreeCla::OnLButtonDown(UINT nFlags, CPoint point)
{
    // Your stuff here
    // blah
    // end your stuff
    CTreeCtrl::OnLButtonDown(nFlags, point);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM