繁体   English   中英

如何找到鼠标的移动方向?

[英]How to find Mouse movement direction?

从按下鼠标的那一刻起,我需要找出鼠标的方向(向左或向右)。

我只能使用事件OnMouseMove。

使用以下方法,我无法获得e.GetPosition(this).X的方向,它与鼠标移动和单击时的值相同。

知道如何解决吗?

    protected override void OnMouseMove(MouseEventArgs e)
    {
        currentPositionX = e.GetPosition(this).X;

        if (e.LeftButton == MouseButtonState.Pressed)
        {
            double deltaDirection = currentPositionX - e.GetPosition(this).X;
            direction = deltaDirection > 0 ? 1 : -1;
        }
    }

您的解决方案即将完成。 在两种情况下,您只需要分别检查当前位置:按下按钮时和不按下按钮时:

protected override void OnMouseMove(MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        double deltaDirection = currentPositionX - e.GetPosition(this).X;
        direction = deltaDirection > 0 ? 1 : -1;
        currentPositionX = e.GetPosition(this).X;
    }
    else
    {
        currentPositionX = e.GetPosition(this).X;
    }
}

向右移动将得到-1,向左移动将返回1。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM