我想在WPF中跟踪鼠标的移动方向(左/右/上/下/对角线)。我看到了this answer on stackoverflow,但对我来说无效。所以我写了一些代码,如下所示 这就是我的逻辑的工作方式。首先,只要鼠标在窗口上移动并且没有按下右键,我都将获得鼠标的前一个位置。当按下鼠标右键并移动鼠标时,我将获得当前位置并将当前位置和先前位置传递给GEtMouseDirection(),该方法将返回方向。问题是,方法在7/10的时间内将方向返回为“ NONE”。但是其他3/10次,它给出了正确的方向,所以我在想的是,鼠标移动事件被频繁触发,以至于“ currentPosition”和“ previousPosition”几乎总是相同。不想发生这种情况,因为我需要准确无误。
还值得注意的是,我已经尝试处理ImageControl和Border的鼠标移动事件。但是却返回相同的结果。因此,我很确定问题不在于我选择在哪里处理鼠标移动事件。
var pTx = imageControl.TransformToAncestor(imageBorder).Transform(new System.Windows.Point(0,0));
现在是为什么我想要获得准确的鼠标移动方向的原因,我正在研究图像查看器,并且想限制例如在图像缩放,鼠标向右和向左移动时的平移图片已与包含我的图片的边框的左对齐。 (也就是pTx.X> = 0时。有人对图像已经对齐时如何获得鼠标移动方向或如何限制平移有更好的解决方案吗?
(“ pre”代表上一个鼠标位置,“ cur”代表当前位置)。
public static MouseDirection GetMouseDirection(Point pre, Point cur)
{
// Mouse moved up
if ((pre.X == cur.X) && (pre.Y > cur.Y))
return MouseDirection.Up;
// Mouse moved down
if ((pre.X == cur.X) && (pre.Y < cur.Y))
return MouseDirection.Down;
// Mouse moved left
if ((pre.X > cur.X) && (pre.Y == cur.Y))
return MouseDirection.Left;
// Mouse moved right
if ((pre.X < cur.X) && (pre.Y == cur.Y))
return MouseDirection.Right;
// Mouse moved diagonally up-right
if ((pre.X < cur.X) && (pre.Y > cur.Y))
return MouseDirection.TopRight;
//return MouseDirection.Up;
// Mouse moved diagonally up-left
if ((pre.X > cur.X) && (pre.Y > cur.Y))
return MouseDirection.TopLeft;
//return MouseDirection.Up;
// Mouse moved diagonally down-right
if ((pre.X < cur.X) && (pre.Y < cur.Y))
return MouseDirection.BottomRight;
//return MouseDirection.Down;
// Mouse moved diagonally down-left
if ((pre.X > cur.X) && (pre.Y < cur.Y))
return MouseDirection.BottomLeft;
//return MouseDirection.Down;
// Mouse didn't move
return MouseDirection.None;
}
答案 0 :(得分:1)
我解决了我的问题。下面是我的代码
鼠标方向枚举
public enum MouseDirection
{
None,
Up,
Down,
Left,
Right,
TopLeft,
TopRight,
BottomLeft,
BottomRight,
}
计算鼠标方向的方法
public static MouseDirection GetMouseDirection(Point pre, Point cur)
{
// Mouse moved up
if ((pre.X == cur.X) && (pre.Y > cur.Y))
return MouseDirection.Up;
// Mouse moved down
if ((pre.X == cur.X) && (pre.Y < cur.Y))
return MouseDirection.Down;
// Mouse moved left
if ((pre.X > cur.X) && (pre.Y == cur.Y))
return MouseDirection.Left;
// Mouse moved right
if ((pre.X < cur.X) && (pre.Y == cur.Y))
return MouseDirection.Right;
// Mouse moved diagonally up-right
if ((pre.X < cur.X) && (pre.Y > cur.Y))
return MouseDirection.TopRight;
//return MouseDirection.Up;
// Mouse moved diagonally up-left
if ((pre.X > cur.X) && (pre.Y > cur.Y))
return MouseDirection.TopLeft;
//return MouseDirection.Up;
// Mouse moved diagonally down-right
if ((pre.X < cur.X) && (pre.Y < cur.Y))
return MouseDirection.BottomRight;
//return MouseDirection.Down;
// Mouse moved diagonally down-left
if ((pre.X > cur.X) && (pre.Y < cur.Y))
return MouseDirection.BottomLeft;
//return MouseDirection.Down;
// Mouse didn't move
return MouseDirection.None;
}
鼠标移动事件处理程序和属性
static System.Windows.Point pre = new System.Windows.Point();
static System.Windows.Point cur = new System.Windows.Point();
private void Panel2_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
cur = e.GetPosition(panel2);
bool mouseMoved = (pre != cur);
if (mouseMoved)
direction = MouseMovement.GetMouseDirection(pre, cur);
pre = cur;
}
else
pre = e.GetPosition(panel2);
}