Search code examples
c++mfcsubclassing

How to notify the parent dialog of a button down message


I want to subclass a CButton to handle the ON_WM_LBUTTONDOWN message.

DownButton.cpp:

#include "stdafx.h" 
#include "DownButton.h" 

//CDownButton 

IMPLEMENT_DYNAMIC(CDownButton, CButton) 

CDownButton::CDownButton() 
{ 
} 

CDownButton::~CDownButton() 
{ 
} 


BEGIN_MESSAGE_MAP(CDownButton, CButton) 
    ON_WM_LBUTTONDOWN() 
END_MESSAGE_MAP() 



// CDownButton message handlers 

void CDownButton::OnLButtonDown(UINT nFlags, CPoint point) 
{ 
}

DownButton.h

#pragma once 


// CDownButton 

class CDownButton : public CButton 
{ 
    DECLARE_DYNAMIC(CDownButton) 

public: 
    CDownButton(); 
    virtual ~CDownButton(); 

protected: 
    DECLARE_MESSAGE_MAP() 
public: 
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point); 
}; 

But how can the dialog containing this button be notified of this happening ? It seems the only message it can receive is ON_BN_CLICKED.


Solution

  • You need to re-send the message to the parent in your OnLButtonDown - Event:

    void CDownButton::OnLButtonDown(UINT nFlags, CPoint point) 
    { 
       // do what you want to do ...
    
       GetParent()->SendMessage(WM_COMMAND, GetDlgCtrlID() | WM_LBUTTONDOWN << 16, (LONG) GetSafeHwnd());
    }