MFC界面中,有些按钮要实现单击事件,还有些按钮要响应ButtonDown和ButtonUp消息?怎么写
我把单击默认的代码改成响应ButtonDown和ButtonUp消息了,如下
BOOL CTestDlg::PreTranslateMessage(MSG* pMsg) 
{
if(pMsg->message == WM_LBUTTONDOWN)
{
if(pMsg->hwnd == GetDlgItem(IDC_BUTTON1)->m_hWnd)
{
                    // 按钮按下
                }
}
else if(pMsg->message == WM_LBUTTONUP)
{
if(pMsg->hwnd == GetDlgItem(IDC_BUTTON1)->m_hWnd)
{
                    // 按钮弹起
                }
} return CDialog::PreTranslateMessage(pMsg);

但怎么两者都能实现呢?

解决方案 »

  1.   


    BOOL CTestDlg::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class CRect rectA,rectB,rectC; GetDlgItem(IDC_BUTTON1)->GetWindowRect(&rectA);
    GetDlgItem(IDC_BUTTON2)->GetWindowRect(&rectB);
    GetDlgItem(IDC_BUTTON3)->GetWindowRect(&rectC); if(pMsg->message == WM_LBUTTONDOWN)
    {
    if(rectA.PtInRect(pMsg->pt))
    {
    MessageBox(_T("A"));
    }
    else if(rectB.PtInRect(pMsg->pt))
    {
    m_strPopText = _T("B");
    }
    else if(rectC.PtInRect(pMsg->pt))
    {
    m_strPopText = _T("C");
    }
    }
    else if(pMsg->message == WM_LBUTTONUP)
    {
    if(rectA.PtInRect(pMsg->pt))
    {
    ;
    }
    else if(rectB.PtInRect(pMsg->pt))
    {
    MessageBox(m_strPopText);
    }
    else if(rectC.PtInRect(pMsg->pt))
    {
    MessageBox(m_strPopText);
    }
    }
    return CDialog::PreTranslateMessage(pMsg);
    }