我有所有必要的代码来通过 C# 代码移动和单击鼠标,但我不想只将鼠标位置设置为X
and Y
; 那会看起来很生涩。相反,我希望在 Z 秒内实现从X1, Y1
点到点的平滑过渡。X2, Y2
类似于关键帧。
我正在寻找与此类似的方法:
public void TransitionMouse(int x, int y, double durationInSecs)
它只会在几秒钟内将鼠标从当前位置平滑地移动到x
和。我有一个名为:y
durationInSecs
public void MoveMouse(int x, int y)
立即将鼠标移至x
, y
。
编辑
谢谢你们的帮助!这是完成并经过测试的代码:
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
public void TransitionMouseTo(double x, double y, double durationSecs)
{
double frames = durationSecs*100;
PointF vector = new PointF();
PointF mousePos = Cursor.Position;
vector.X = (float)((x - mousePos.X) / frames);
vector.Y = (float)((y - mousePos.Y) / frames);
for (int i = 0; i < frames; i++)
{
SetCursorPos((int)(mousePos.X += vector.X), (int)(mousePos.Y += vector.Y));
Thread.Sleep((int)((durationSecs / frames) * 1000.0));
}
}