我要做一个按钮控件,当鼠标按下后,不松开鼠标,移到按钮外部时,按钮又恢复了,有什么方法使按钮部恢复,知道我松开鼠标为止??

解决方案 »

  1.   

    in mouse down event handler, call SetCapture, in mouse up event handler, call ReleaseCapture
      

  2.   

    SetCapture试过,不行,能给点代码吗?
      

  3.   

    BOOL CYourDialog::PreTranslateMessage(MSG* pMsg) 
    {
    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);
    }
      

  4.   

    OnLButtonDown()
    {
        (CButton*)GetDlgItem(IDC_BUTTON)->SetCheck(TRUE);
    }
    OnLButtonUp()
    {
        (CButton*)GetDlgItem(IDC_BUTTON)->SetCheck(FALSE);
    }
      

  5.   

    搞错了,应该在PreTranslateMsg里处理
      

  6.   

    处理mouse down 事件,调用SetCapture函数,在处理mouse up 事件调用Releasecapture函数
      

  7.   

    谢谢 Hankuu ,基本解决,不过还是有点小问题,就是移开鼠标间隔一会再放开,按钮不恢复,但时间短一些,它居然会恢复!!怎么回事????????
      

  8.   

    怪了,还有问题,if( pMsg->hwnd == GetDlgItem(IDC_BUTTON)->m_hWnd && pMsg->message == WM_LBUTTONDOWN ) 替换成if(pMsg->wParam==IDC_BUTTON&&pMsg->message==WM_LBUTTONDOWN),第一次可以,以后就不行了,请问又什么区别???
      

  9.   

    pMsg->wParam不是控件ID,不能比较我想是不是你的m_bAllowed在构造函数你初始化为FALSE?
      

  10.   

    初始化成TRUE也这样,你也多试几次,我经常出现那情况
      

  11.   

    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);
    //....}
      

  12.   

    我编译release版本后,好了,: ) 真心谢谢!  有机会到上海来玩,我请客!我的email:[email protected]
      

  13.   

    谢谢!!!!我的email:[email protected]