有效方法:
BOOL CButtontestDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message==WM_LBUTTONDOWN)
{
if (pMsg->hwnd == GetDlgItem(IDC_BUTTON1)->m_hWnd)
{
SetDlgItemText(IDC_STATIC_SHOW1,"BUN1 DOWN");
}
}
if (pMsg->message==WM_LBUTTONUP)
{
if (pMsg->hwnd == GetDlgItem(IDC_BUTTON1)->m_hWnd)
{
SetDlgItemText(IDC_STATIC_SHOW1,"BUN1 UP");
}
}
return CDialog::PreTranslateMessage(pMsg);
}
补充:设置BUTTON控件按下 抬起效果
(CButton*)GetDlgItem(IDC_BUTTON_UP) ->SetState(TRUE)
(CButton*)GetDlgItem(IDC_BUTTON_UP) ->SetState(FALSE)
还有以下办法,但是尝试后无法实现,目前还不知道原因
===================================================
获得按钮区域:
CButton m_Button1;
CRect rc;
m_Button1.GetWindowRect(rc);

这篇博客介绍了如何在MFC应用中使BUTTON控件响应鼠标按下(WM_LBUTTONDOWN)和抬起(WM_LBUTTONUP)事件。通过重载`PreTranslateMessage`函数来实现对IDC_BUTTON1的点击状态显示更新。同时提到了使用`SetState`方法设置按钮的按下和抬起效果,以及尝试通过`OnLButtonDown`和`OnMouseMove`响应消息但未成功的问题。
最低0.47元/天 解锁文章
7733





