//CButtonEx.h
class CButtonEx : public CButton
{DECLARE_DYNAMIC(CButtonEx)
public:
CButtonEx();
virtual ~CButtonEx();
protected:
DECLARE_MESSAGE_MAP()
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
public:
void SetButtonEnable(BOOL bEnable = TRUE);
private:
BOOL m_bEnable;
};
//CButtonEx.cpp
#include "ButtonEx.h"
IMPLEMENT_DYNAMIC(CButtonEx, CButton)
CButtonEx::CButtonEx()
: m_bEnable(TRUE)
{
}
CButtonEx::~CButtonEx()
{
}
BEGIN_MESSAGE_MAP(CButtonEx, CButton)
ON_WM_KEYDOWN()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()
// CButtonEx message handlers
BOOL CButtonEx::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN
|| pMsg->message == WM_KEYUP)
{
if (!m_bEnable && (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE))
{
return TRUE;
}
}
return CButton::PreTranslateMessage(pMsg);
}
void CButtonEx::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CButton::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CButtonEx::OnLButtonDown(UINT nFlags, CPoint point)
{
if(!m_bEnable)
{
return;
}
CButton::OnLButtonDown(nFlags, point);
}
void CButtonEx::OnLButtonDblClk(UINT nFlags, CPoint point)
{
if(!m_bEnable)
{
return;
}
CButton::OnLButtonDblClk(nFlags, point);
}
void CButtonEx::SetButtonEnable(BOOL bEnable)
{
m_bEnable = bEnable;
}