2011-05-05 52 views
3

我想在两个坐标之间以编程方式移动鼠标。 但我想在所有快速或慢速加工机器上可靠地保持速度。 我看到this link here。但是当模拟两个坐标之间的移动时,它并不保证光标的最佳,平滑和可见的速度。我不知道是否有人知道一个技巧,以确定像各种机器的延迟和步骤最佳值的参数像我的第一个想法是使用特定迭代的for-loop来确定机器的性能,然后根据多少时间for - 洛普采取了一个想法?或者我完全错了吗? 谢谢模拟鼠标光标在两个坐标之间的C#中移动

回答

3

您应该使运动成为时间的函数。最佳答案开始在C# moving the mouse around realistically,并使用秒表类测量经过时间:

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); 
} 
+0

这看起来不错,但我无法用这个算法以更慢的速度抓住。它变得瞬间。我用不同的Timespan和延迟值来衡量它。 – Deku 2011-05-05 21:27:30

+0

@Deku - 我看到你在评论后一两个小时接受了答案。你知道有什么不对吗?我没有测试这个代码;它需要任何改变吗? – Justin 2011-05-06 15:12:02

+0

..我给了它一个答案,因为它是一个更好的解决方案。但没有完全回答我的问题,以上问题。这里是测试它的代码。 – Deku 2011-05-06 17:40:16

0

我会推荐一些物理学。速度是距离除以时间。如果你想在每台机器上使用合适的鼠标速度,你必须得到准确的时间。

让我们做出了榜样:

希望将鼠标从点0/0移至400/600和端点应始终在3秒后到达。因此,你必须保存开始时间并建立一个while循环,这将在starttime + 3s结束。在循环中,根据已用时间和总时间计算X和Y坐标。

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

这将是机器独立。对于一个好的结果,你应该使用高精度的时间,如Stopwatch

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(); 
     }  
    } 
}