1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| 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, 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...
}
}
} |
Partager