3

我正在尝试以编程方式在两个坐标之间移动鼠标。但我想在所有快速或慢速处理机器上可靠地保持速度。我在这里看到了这个链接。但它不能保证在模拟两个坐标之间的移动时光标的最佳、平滑和可见的速度。我想知道是否有人知道一种技巧来确定各种机器的延迟和步数等参数的最佳值就像我的第一个想法是使用 for-loop 进行特定的迭代来确定机器的性能然后根据 for 的时间对参数进行评分-loop 采取了......一个想法?还是我完全错了?谢谢

4

3 回答 3

4

You should make the motion a function of time. Starting with the answer at C# moving the mouse around realistically, and using the Stopwatch class to measure the elapsed time:

public void LinearSmoothMove(Point newPosition, TimeSpan duration) 
{
    Point start = GetCursorPosition();

    // Find the vector between start and newPosition
    double deltaX = newPosition.X - start.X;
    double deltaY = newPosition.Y - start.Y;

    // start a timer
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();

    double timeFraction = 0.0;

    do
    {
        timeFraction = (double)stopwatch.Elapsed.Ticks / duration.Ticks;
        if (timeFraction > 1.0)
            timeFraction = 1.0;

        PointF curPoint = new PointF(start.X + timeFraction * deltaX, 
                                     start.Y + timeFraction * deltaY);
        SetCursorPosition(Point.Round(curPoint));
        Thread.Sleep(20);
    } while (timeFraction < 1.0);
}
于 2011-05-05T19:13:26.663 回答
0

我试过这个,但仍然不是最佳的。它仍然随机器处理能力而变化。@贾斯汀,对持续时间和睡眠时间使用不同的值。如果您在测试后提出更好的解决方案,请告诉我。谢谢!

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication11
{
   class Program
    {

       [DllImport("user32.dll")]
       static extern bool SetCursorPos(int X, int Y);   

       public static void LinearSmoothMove(Point newPosition, TimeSpan duration)
       {
           Point start = Cursor.Position;
           int sleep = 10;
           //PointF iterPoint = start;
           // Find the vector between start and newPosition   
           double deltaX = newPosition.X - start.X;
           double deltaY = newPosition.Y - start.Y;
           // start a timer    
           Stopwatch stopwatch = new Stopwatch();
           stopwatch.Start();
           double timeFraction = 0.0;
           do
           {
               timeFraction = (double)stopwatch.Elapsed.Ticks / duration.Ticks;
               if (timeFraction > 1.0)
                timeFraction = 1.0;
               PointF curPoint = new PointF((float)(start.X + timeFraction * deltaX), (float)(start.Y + timeFraction * deltaY));
               SetCursorPos(Point.Round(curPoint).X, Point.Round(curPoint).Y);
               Thread.Sleep(sleep);
           } while (timeFraction < 1.0);
       }
       static void Main(string[] args)
       {
             TimeSpan delayt = new  TimeSpan(0, 0, 3);
           LinearSmoothMove(new Point(20, 40), delayt);
           Console.Read();
        }       
    }
}
于 2011-05-06T17:51:36.993 回答
0

我会推荐一些物理。速度是距离除以时间。如果您想在每台机器上保持恒定的鼠标速度,您必须获得准确的时间。

让我们举个例子:

您想将鼠标从点 0/0 移动到 400/600,并且应该始终在 3 秒后到达端点。

因此,您必须保存开始时间并构建一个 while 循环,该循环将在 starttime + 3s 结束。在循环中,您根据经过的时间和总时间计算 X 和 Y 坐标。

X = 400 / 3s * ElapsedTime
Y = 600 / 3s * ElapsedTime

这将与机器无关。为了获得好的结果,您应该使用像Stopwatch这样的高精度时间。

于 2011-05-05T19:15:04.453 回答