using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Drawing; using System.Windows.Forms; namespace ConsoleApplication2 { class source { private const uint WM_LBUTTONDOWN = 0x0201; private const uint WM_LBUTTONUP = 0x0202; private static readonly IntPtr MK_LBUTTON = new IntPtr(0x0001); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] static extern bool ScreenToClient(IntPtr hWnd, ref Point lpPoint); private class NativeMethods { [DllImport("user32.dll", SetLastError = false)] internal static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); } [DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern int FindWindow(string sClass, string sWindow); /* Le programme se lance avec la ligne de commande suivante : simulation_clic.exe "titre de la fenêtre" X_absolu Y_absolu */ static void Main(string[] args) { // Récupération des paramètres et modification des types de variables string titre_fenetre = args[0]; int X_absolu = int.Parse(args[1]); int Y_absolu = int.Parse(args[2]); // Détermination du handle de la fenêtre en fonction de son titre int handle_int = FindWindow(null, titre_fenetre); IntPtr handle = new IntPtr(handle_int); // Conversion des deux 'int' en un 'Point' Point coord_fenetre = new Point(X_absolu, Y_absolu); // Transformation des coordonnées absolues en coordonnées relatives à la fenêtre ScreenToClient(handle, ref coord_fenetre); // ... Jusque là c'est ok ! // Mise au point du lParam IntPtr lParam = new IntPtr(coord_fenetre.X | (coord_fenetre.Y << 16)); // Simulation du clic NativeMethods.PostMessage(handle, WM_LBUTTONDOWN, MK_LBUTTON, lParam); NativeMethods.PostMessage(handle, WM_LBUTTONUP, IntPtr.Zero, lParam); // Le clic ne s'exécute pas... } } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question