my method requires you to subclass the button, you need to create a CYourButton based on CButton, and override WM_LBUTTONDOWN, WM_LBUTTONUP, and WM_GETDLGCODE:void CYourButton::OnLButtonDown(UINT nFlags, CPoint point) { SetCapture(); SetState(TRUE); }void CYourButton::OnLButtonUp(UINT nFlags, CPoint point) { SetState(FALSE); if (m_hWnd == ::GetCapture()) { ReleaseCapture(); if (CWnd* pParent = GetParent()) { pParent->SendMessage(WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), BN_CLICKED), (LPARAM)m_hWnd); } } }UINT CYourButton::OnGetDlgCode() { return DLGC_BUTTON; }then in your CYourDialog.h#include "YourButton.h"class CYourDialog: public CDialog { CYourButton m_myButton; };in YourDialog.CPP: BOOL CYourDialog::OnInitDialog() { //.... m_myButton.SubclassDlgItem(IDC_YOURBUTTONIDHERE,this); //....}
{
if( pMsg->hwnd == GetDlgItem(IDC_BUTTON)->m_hWnd && pMsg->message == WM_LBUTTONDOWN )
{
m_bAllowed = FALSE;
}
else if( pMsg->message == WM_MOUSEMOVE && !m_bAllowed )
{
return TRUE;
}
else if(pMsg->message == WM_LBUTTONUP)
m_bAllowed = TRUE;
return CDialog::PreTranslateMessage(pMsg);
}
{
(CButton*)GetDlgItem(IDC_BUTTON)->SetCheck(TRUE);
}
OnLButtonUp()
{
(CButton*)GetDlgItem(IDC_BUTTON)->SetCheck(FALSE);
}
WM_LBUTTONDOWN, WM_LBUTTONUP, and WM_GETDLGCODE:void CYourButton::OnLButtonDown(UINT nFlags, CPoint point)
{
SetCapture();
SetState(TRUE);
}void CYourButton::OnLButtonUp(UINT nFlags, CPoint point)
{
SetState(FALSE);
if (m_hWnd == ::GetCapture())
{
ReleaseCapture();
if (CWnd* pParent = GetParent())
{
pParent->SendMessage(WM_COMMAND,
MAKEWPARAM(GetDlgCtrlID(), BN_CLICKED), (LPARAM)m_hWnd);
}
}
}UINT CYourButton::OnGetDlgCode()
{
return DLGC_BUTTON;
}then in your CYourDialog.h#include "YourButton.h"class CYourDialog: public CDialog
{
CYourButton m_myButton;
};in YourDialog.CPP:
BOOL CYourDialog::OnInitDialog()
{
//....
m_myButton.SubclassDlgItem(IDC_YOURBUTTONIDHERE,this);
//....}