Ajouter préalablement ceci dans votre class
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo); private const int MOUSEEVENTF_LEFTDOWN = 0x02; private const int MOUSEEVENTF_LEFTUP = 0x04; private const int MOUSEEVENTF_RIGHTDOWN = 0x08; private const int MOUSEEVENTF_RIGHTUP = 0x10;
puis utiliser ce bout de code sur un bouton , timer ou autre événement
//simule un clic a la position courante du curseur int X = Cursor.Position.X; int Y = Cursor.Position.Y; mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
Exemple programme
using System; using System.Windows.Forms; using System.Runtime.InteropServices; namespace CliqueSouris { public partial class Form1 : Form { [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo); private const int MOUSEEVENTF_LEFTDOWN = 0x02; private const int MOUSEEVENTF_LEFTUP = 0x04; private const int MOUSEEVENTF_RIGHTDOWN = 0x08; private const int MOUSEEVENTF_RIGHTUP = 0x10; public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { textBox2.Text = "Auto-Clique Start"; Decimal valeur = 60000 * numericUpDown1.Value; timer1.Interval = Decimal.ToInt32(valeur); timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { string Date = DateTime.Now.ToString(); try { //simule un clic a la position courante du curseur int X = Cursor.Position.X; int Y = Cursor.Position.Y; mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0); textBox1.AppendText(Date + " Clique done!"); } catch { } } private void button1_Click(object sender, EventArgs e) { textBox2.Text="Auto-Clique Stop"; timer1.Stop(); } } }
Petit programme qui clique sur la position du curseur à intervalle défini (selon ce que vous choisissez )