Button控件在ClassWizard里默认只有单击和双击事件 我想给BUTTON添加按下和抬起事件
请问是如何添加的啊!!!

解决方案 »

  1.   

    子类化一个按钮类:
    #define WUSER_LBUTTON_DOWN   WM_USER+1
    #define WUSER_LBUTTON_UP     WM_USER+2CMsgButton :public CButton
    {
    ....................
      //{{AFX_VIRTUAL(CMsgButton)
    public:
    virtual BOOL PreTranslateMessage(MSG* pMsg);
    //}}AFX_VIRTUAL
    ....................
    }
    ///////////////////////////////
    BOOL CMsgButton::PreTranslateMessage(MSG* pMsg)
    {
       CWnd* pParent = GetParent();
       if(pMsg->message == WM_LBUTTONDOWN)
       {
             ::PostMessage(pParent->GetSafeHwnd(),WUSER_LBUTTON_DOWN,0,0);
       }
       else if(pMsg->message == WM_LBUTTONUP)
       {
             ::PostMessage(pParent->GetSafeHwnd(),WUSER_LBUTTON_UP,0,0);  
       }   return CButton::PreTranslateMessage(pMsg);
    }
    /////////////////////////////////////////////////////////////
    在对话框类中,为ID为IDC_BUTTONX的控件绑定一个CMsgButton类的成员变量
    然后添加自定义消息响应函数到对话框.H,和.CPP文件
    在H文件中:
    //{{AFX_MSG(CTestClass)
    virtual BOOL OnInitDialog();
             ....................
    //}}AFX_MSG
    afx_msg void OnButtonLDown(WPARAM,LPARAM);
           DECLARE_MESSAGE_MAP()
    在CPP文件中:
    BEGIN_MESSAGE_MAP(CTestClass, CDialog)
    //{{AFX_MSG_MAP(CTestClass)
             。  
    //}}AFX_MSG_MAP
    ON_MESSAGE(WUSER_LBUTTON_DOWN,OnButtonLDown)
    END_MESSAGE_MAP()函数实现
    void CTestClass::OnButtonLDown(WPARAM,LPARAM)
    {
        MessageBox(_T("lbuttondown"));
    }