鼠标按下拖动改变及滚轮改变Edit值

class CMy_Edit : public CEdit
{
    DECLARE_DYNAMIC(CMy_Edit)

public:
    CMy_Edit();
    virtual ~CMy_Edit();
    bool m_bLButtonDown;
    long m_nOrign_y;
    long m_nMove_y;
protected:
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
};


void CMy_Edit::OnLButtonDown(UINT nFlags, CPoint point)
{
    m_nOrign_y = point.y;//鼠标左键按下后记录按下的y值,并设置按下标志为true
    m_bLButtonDown = true;
    CEdit::OnLButtonDown(nFlags, point);
}

void CMy_Edit::OnLButtonUp(UINT nFlags, CPoint point)
{
    m_bLButtonDown = false;//设置按下标志为false
    CEdit::OnLButtonUp(nFlags, point);
}

void CMy_Edit::OnMouseMove(UINT nFlags, CPoint point)
{   
    if (m_bLButtonDown) {//如果鼠标左键按下了且鼠标滚动了,根据鼠标滚动前后的值更新edit的值
        m_nMove_y = point.y;
        int offset = (m_nOrign_y - m_nMove_y);
        //原始值
        CString str;
        this->GetWindowTextW(str);
        int val = _ttoi(str);
        //增加
        val += offset;
        str.Format(_T("%d"), val);
        this->SetWindowTextW(str);

    }
    CEdit::OnMouseMove(nFlags, point);
}

BOOL CMy_Edit::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{   
    int offset = (zDelta/12);//滚动一下改变120,120/12=10
    //原始值
    CString str;
    this->GetWindowTextW(str);
    int val = _ttoi(str);
    //增加10
    val += offset;
    str.Format(_T("%d"), val);
    this->SetWindowTextW(str);
    return CEdit::OnMouseWheel(nFlags, zDelta, pt);
}

你可能感兴趣的:(VC++)