[MFC]MFC的BUTTON控制元件響應滑鼠按下和擡起
有效方法:
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);
響應:
void CYourDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//判斷滑鼠在哪個按鈕區域中
if(PtInRect(rc, point))
{
}
CDialog::OnLButtonDown(nFlags, point);
===================================================
需要過載CButton類並且將該類子類化才可以響應訊息WM_DOWN與WM_UP訊息.
=================================================================
繼承CButton,新增OnLButtonDown hold = true; OnLButtonUp holdstate = false;
OnMouseMove 中檢測滑鼠位置,如果移出按鈕,holdstate=false;