如何消除 C# WPF 中鼠标移动的像素噪声

发布于 2024-12-26 03:22:28 字数 566 浏览 1 评论 0原文

当我没有远程控制时,我遇到了鼠标停止的时刻,但现在我远程控制了光标。现在,我无法获得鼠标停止的时刻,因为它永远不会完全停止,它总是在移动一点点。我的想法是添加一些偏移像素。比如5px,即使鼠标移动了5px左右,也会说鼠标停止了。

这是捕获鼠标停止时刻的代码的一部分。它会触发一个计时器。我没有把计时器代码放在这里。

 mouse = new MouseKeyboardActivityMonitor.MouseHookListener(new GlobalHooker());
 mouse.MouseMove += (sd, args) =>
 {
      movingCount = 0;
      mouseLeft = args.X; //set the window.left to mouseLeft before showing it
      mouseTop = args.Y; //set the window.top to mouseTop before showing it
 };

 mouse.Enabled = true;

对于获得鼠标不停移动但移动 5 px 噪音的时刻,您有什么想法?

I was getting the moment when mouse stops while I was not controlling the remotely, but now I control the cursor remotely. Right now, I can not get the moment when mouse stops, because It never exactly stops, it is always moving a little bit. My idea is to add some offset pixels. For example 5 px, even the mouse moves around 5 px, It will say that mouse stopped.

This was the some part of the code to capture the moment mouse stops. It triggers a timer. I didn't put here the timer code.

 mouse = new MouseKeyboardActivityMonitor.MouseHookListener(new GlobalHooker());
 mouse.MouseMove += (sd, args) =>
 {
      movingCount = 0;
      mouseLeft = args.X; //set the window.left to mouseLeft before showing it
      mouseTop = args.Y; //set the window.top to mouseTop before showing it
 };

 mouse.Enabled = true;

What are your ideas about getting the moment when mouse doesn't stop but moving around 5 px noise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヤ经典坏疍 2025-01-02 03:22:28

根据评论中的要求,这里有一个示例:

您可以使用系统参数MinimumVerticalDragDistance和MinimumHorizo​​ntalDragDistance。

请参阅此处: http://msdn.microsoft.com/ en-us/library/system.windows.systemparameters.minimumverticaldragdistance.aspx

它们最初用作 DragDrop 操作中的阈值,以检测拖动是否成功是否真正开始,或者如果用户仅单击并将鼠标指针移动很小的距离。

使用方法如下:

private Point _lastPosition;

private void Window_MouseMove(object sender, MouseEventArgs e)
{
  Point currentPosition = e.GetPosition(this);

  if (IsMovementBigEnough(_lastPosition, currentPosition))
  {
    // .. do stuff here 
  }

  _lastPosition = currentPosition;
}

public bool IsMovementBigEnough(Point previousMousePosition, Point currentPosition)
{
  return (Math.Abs(currentPosition.X - previousMousePosition.X) >= SystemParameters.MinimumHorizontalDragDistance ||
       Math.Abs(currentPosition.Y - previousMousePosition.Y) >= SystemParameters.MinimumVerticalDragDistance);
}

HTH

As requested in the comments here's an example:

You can use the System Parameters MinimumVerticalDragDistance and MinimumHorizontalDragDistance.

See here: http://msdn.microsoft.com/en-us/library/system.windows.systemparameters.minimumverticaldragdistance.aspx

They are originally used as a threshold in DragDrop Operations to detect if the drag is really beginning or if the user was only clicking and moving the mouse pointer a very small distance.

Here's how it could be used:

private Point _lastPosition;

private void Window_MouseMove(object sender, MouseEventArgs e)
{
  Point currentPosition = e.GetPosition(this);

  if (IsMovementBigEnough(_lastPosition, currentPosition))
  {
    // .. do stuff here 
  }

  _lastPosition = currentPosition;
}

public bool IsMovementBigEnough(Point previousMousePosition, Point currentPosition)
{
  return (Math.Abs(currentPosition.X - previousMousePosition.X) >= SystemParameters.MinimumHorizontalDragDistance ||
       Math.Abs(currentPosition.Y - previousMousePosition.Y) >= SystemParameters.MinimumVerticalDragDistance);
}

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文