获取鼠标移动的距离

本文关键字:距离 移动 鼠标 获取 | 更新日期: 2023-09-27 17:59:50

我有一个表单,里面有一个小的Panel,叫做Player。如何将面板"链接"到用户的鼠标,使其随着鼠标的移动而移动?

我已经订阅了Player_MouseMovePlayer.MouseMove事件,但我不知道鼠标实际移动了多少。我唯一能想到的方法就是有一个这样的:

private Point previousLocation;
private void Player_MouseMove(object sender, MouseEventArgs e)
{
    int differenceX, differenceY;
    differenceX = e.X - previousLocation.X;
    differenceY = e.Y - previousLocation.Y;
    previousLocation = e.Location;
}

这看起来很愚蠢,每次都有一个额外的变量并计算差值。完美的方法应该是Player.LinkToCursor();之类的,但如果没有自动化的方法,至少有更好的方法吗?

获取鼠标移动的距离

查看http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs.aspx我看不出有什么能帮你把这件事做得更好。

然而,有一件事你可以做:

Point difference = e.Location - (Size)previousLocation;

矢量算法;)