Ce qui suit régler la position de la souris et effectuer un clic:
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);
Il suffit de garder à l'esprit que cela pourrait être extrêmement gênant pour un utilisateur.
:)
Si vous voulez cliquer sur un bouton sur votre formulaire, vous pouvez utiliser la méthode 'PerformClick()'
.
La question qui saute immédiatement à l'esprit est «pourquoi»? –
de faire quelque chose dans ma tête. Pourquoi? – Sherif
@MatthewScharley Je suis venu ici pour "l'automatisation" .. –