C#でマウスカーソルの位置を画面上の指定されたポイントに設定するにはどうすればよいですか?
マウスとキーボードの座標を受け取り、押すマザーボードバッファをハックする必要がありますか?
クリックを行うための別のものがありますか、私は想像していますか?
C#でマウスカーソルの位置を画面上の指定されたポイントに設定するにはどうすればよいですか?
マウスとキーボードの座標を受け取り、押すマザーボードバッファをハックする必要がありますか?
クリックを行うための別のものがありますか、私は想像していますか?
以下は、マウスの位置を設定し、クリックを実行します。
public static void ClickSomePoint() {
// Set the cursor position
System.Windows.Forms.Cursor.Position = new Point(20, 35);
DoClickMouse(0x2); // Left mouse button down
DoClickMouse(0x4); // Left mouse button up
}
static void DoClickMouse(int mouseButton) {
var input = new INPUT() {
dwType = 0, // Mouse input
mi = new MOUSEINPUT() { dwFlags = mouseButton }
};
if (SendInput(1, input, Marshal.SizeOf(input)) == 0) {
throw new Exception();
}
}
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT {
int dx;
int dy;
int mouseData;
public int dwFlags;
int time;
IntPtr dwExtraInfo;
}
struct INPUT {
public uint dwType;
public MOUSEINPUT mi;
}
[DllImport("user32.dll", SetLastError=true)]
static extern uint SendInput(uint cInputs, INPUT input, int size);
これはユーザーにとって非常に迷惑になる可能性があることに注意してください。
:)
フォームのボタンをクリックする場合は、この方法を使用できます'PerformClick()'
。
Windows10でマウスの位置を取得および設定します。
Cursor.Positionプロパティを使用したc#.NETFramework4.0でははるかに簡単です
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.cursor.position?view=netcore-3.1
public static void ClickSomePoint()
{
// get mouse position
System.Drawing.Point screenPos = System.Windows.Forms.Cursor.Position;
// create X,Y point (0,0) explicitly with System.Drawing
System.Drawing.Point leftTop = new System.Drawing.Point(0,0);
// set mouse position
Cursor.Position = leftTop;
Console.WriteLine(screenPos);
}
マウスを配置する最も簡単な方法:
左の位置は左ゼロ、上の位置は右ゼロ(任意の数に変更できます)(このコードを使用するには、使用する必要のある名前空間using System.Drawing
とusing System.Windows.Forms
名前空間)
Cursor.Position = new Point(0,0);