鼠标按下拖动改变及滚轮改变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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值