Winforms で UI スレッドをロックせずにカーソルを移動する方法があるかどうかを知りたいです。言い換えると; 非同期ソリューション。
私の現在の同期ソリューション:
private void Form1_Load(object sender, EventArgs e)
{
TimeSpan delayt = new TimeSpan(0, 0, 3);
LinearSmoothMove(new Point(20, 40), delayt);
}
[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;
double deltaX = newPosition.X - start.X;
double deltaY = newPosition.Y - start.Y;
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);
}