Search code examples
c++windowsmfcwindow-messages

How can I handle control messages on the parent CDialog?


I've got some control like CtrlTree on CMyDialog. I want to handle messages like ON_WM_LBUTTONDOWN() from CTreeCtrl in the CMyDialog class.

Is there any way in MFC to redirect message stream to parent?


Solution

  • The simplest way to redirect your messages is just sending a custom (WM_USER + xxx) message from your control's ON_WM_LBUTTONDOWN handler to the parent class.

    Place parent's WM_LBUTTONDOWN handler code in a separate method and call this method directly.

    Something like that (pseudo code), presuming that your existing code sits in HandleTreeCtrlLBDown()

    CMyTreeCtrl::OnLButtonDown(..)
    {
       pParent ->SendMessage(WM_TREECTRLLBDOWN, 0, (LPARAM)this);
    }
    
    CControlParentDialog::OnTreeCtrlLBDown(wParam, lParam)
    {
       HandleTreeCtrlLBDown();
    }