x秒ごとにマウスの動きをシミュレートしたい。そのために、タイマー(x秒)を使用し、タイマーが作動したらマウスを動かします。
しかし、C#を使用してマウスカーソルを移動させるにはどうすればよいですか?
x秒ごとにマウスの動きをシミュレートしたい。そのために、タイマー(x秒)を使用し、タイマーが作動したらマウスを動かします。
しかし、C#を使用してマウスカーソルを移動させるにはどうすればよいですか?
回答:
Cursor.Positionプロパティを見てください。それはあなたが始めるはずです。
private void MoveCursor()
{
   // Set the Current cursor, move the cursor's Position,
   // and set its clipping rectangle to the form. 
   this.Cursor = new Cursor(Cursor.Current.Handle);
   Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
   Cursor.Clip = new Rectangle(this.Location, this.Size);
}
Cursor.Clipマウスの動きをLocationとで指定されたサイズに制限しますSize。したがって、上記のスニペットでは、マウスがアプリケーションの境界ボックス内でのみ移動できます。
                    Cursor.Position仮想マシンで使用する場合、特定の設定が必要になる場合があります。
                    まず、Win32.csというクラスを追加します
public class Win32
{ 
    [DllImport("User32.Dll")]
    public static extern long SetCursorPos(int x, int y);
    [DllImport("User32.Dll")]
    public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);
    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;
        public POINT(int X, int Y)
        {
            x = X;
            y = Y;
        }
    }
}
あなたはそれを次のように使うことができます:
Win32.POINT p = new Win32.POINT(xPos, yPos);
Win32.ClientToScreen(this.Handle, ref p);
Win32.SetCursorPos(p.x, p.y);