Comment effectuer une souris virtuelle, cliquez sur C # sans utiliser de souris

Je voudrais effectuer un clic dans une application Windows, sans utiliser la vraie souris (afin que je puisse le minimiser). Un peu comme un bot se comporterait.

Comment je ferais ça?

Je pense que la fonction que vous recherchez est PostMessage

 [DllImport("user32.dll", SetLastError = true)] public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam); 

Vous pouvez en savoir plus ici sur codeproject et télécharger un projet de démonstration qui envoie des frappes au clavier.

Cette méthode poste les messages directement dans la file d’entrée associée au programme, en fonction du descripteur de processus que vous utilisez (hWnd).

Vous pouvez également utiliser cette fonction pour envoyer des clics de souris avec elle, en publiant des événements de boutons, comme suit:

 PostMessage(hWnd, WM_LBUTTONDBLCLK, 0, l); 

Vous trouverez plus d’informations sur ces événements de bouton ici sur MSDN .

Je suis sûr que si vous effectuez une recherche sur Internet pour obtenir des échantillons d’événements de souris PostMessage, vous en trouverez de nombreuses

Le code ci-dessous est un repost à partir d’ ici :

 using System; using System.Windows.Forms; using System.Runtime.InteropServices; public class Form1 : Form { [DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint 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() { } public void DoMouseClick() { //Call the imported function with the cursor's current position int X = Cursor.Position.X; int Y = Cursor.Position.Y; mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0); } //...other code needed for the application } 

Lorsque je simule des touches et des clics de souris, j’utilise Windows Input Simulator.

Vous pouvez utiliser un chronomètre ou un appui sur une touche et créer dans le tic-tac du timer ou une fonction d’appuyer sur un bouton, simuler un clic de souris à l’aide du fichier user32.dll (il est préférable de prendre la forme d’un formulaire pour pouvoir gérer l’intervalle du timer …):

 using System; using System.Windows.Forms; namespace Clicker { public partial class Form1 : Form { [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); private const int MOUSEEVENT_LEFTDOWN = 0x02; private const int MOUSEEVENT_LEFTUP = 0x04; private const int MOUSEEVENT_MIDDLEDOWN = 0x20; private const int MOUSEEVENT_MIDDLEUP = 0x40; private const int MOUSEEVENT_RIGHTDOWN = 0x08; private const int MOUSEEVENT_RIGHTUP = 0x10; private int count = 0; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { timer1.Start(); label2.Text = "Timer is on"; } private void button2_Click(object sender, EventArgs e) { timer1.Stop(); label2.Text = "Timer is off"; } private void timer1_Tick(object sender, EventArgs e) { mouse_event(MOUSEEVENT_LEFTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENT_LEFTUP, 0, 0, 0, 0); count++; label3.Text = count + " amount of clicks"; } } 

}

Vous aurez besoin de la résolution de la machine sur laquelle elle est System.Windows.Forms.Screen , utilisez la classe System.Windows.Forms.Screen , ici: SO

Vous devrez ensuite déplacer la souris vers cet emplacement ou, pour éviter cela, vous devrez peut-être vous connecter au programme en cours d’exécution et lui envoyer un événement qui le réduira au minimum.

Il sera difficile d’obtenir quelque chose comme cela avec C #, car vous aurez besoin d’injecter cette DLL dans le programme. Un langage de niveau inférieur comme C peut être utile.

Voici une brève explication / question

 [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); public const int MOUSEEVENTF_LEFTDOWN = 0x02; public const int MOUSEEVENTF_LEFTUP = 0x04; public const int MOUSEEVENTF_RIGHTDOWN = 0x08; public const int MOUSEEVENTF_RIGHTUP = 0x10; public void MouseClick() { int x = 100; int y = 100; mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0); } 

J’ai trouvé cela à l’ adresse http://social.msdn.microsoft.com/forums/en-US/winforms/thread/86dcf918-0e48-40c2-88ae-0a09797db1ab/ .

Vous pouvez utiliser Window Automation pour rechercher le bouton Réduire dans la fenêtre et effectuer un clic. Je trouve cela facile et beaucoup utilisé. voici le lien pour comprendre le concept entier. https://msdn.microsoft.com/en-us/library/windows/desktop/ff486375(v=vs.85).aspx