如何通知按钮按下消息的父对话框

时间:2015-07-29 15:17:39

标签: c++ mfc subclassing

我想将CButton子类化以处理ON_WM_LBUTTONDOWN消息。

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); 
}; 

但是如何通知包含此按钮的对话框?它似乎唯一能收到的消息是ON_BN_CLICKED。

1 个答案:

答案 0 :(得分:1)

您需要将消息重新发送给OnLButtonDown中的父级 - 事件:

void CDownButton::OnLButtonDown(UINT nFlags, CPoint point) 
{ 
   // do what you want to do ...

   GetParent()->SendMessage(WM_COMMAND, GetDlgCtrlID() | WM_LBUTTONDOWN << 16, (LONG) GetSafeHwnd());
}