如何将按钮按下消息通知父对话框
How to notify the parent dialog of a button down message
我想子类化一个 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.
您需要在 OnLButtonDown 中将消息重新发送给父项 - 事件:
void CDownButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// do what you want to do ...
GetParent()->SendMessage(WM_COMMAND, GetDlgCtrlID() | WM_LBUTTONDOWN << 16, (LONG) GetSafeHwnd());
}
我想子类化一个 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.
您需要在 OnLButtonDown 中将消息重新发送给父项 - 事件:
void CDownButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// do what you want to do ...
GetParent()->SendMessage(WM_COMMAND, GetDlgCtrlID() | WM_LBUTTONDOWN << 16, (LONG) GetSafeHwnd());
}