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
|
// Positionne la fenêtre en avant-plan :
Procedure TForm1.SetForeground(Handle : Hwnd);
begin
If IsIconic(Handle) Then
ShowWindow(Handle,SW_RESTORE);
SetForegroundWindow(Handle);
end;
// Envoi d'une chaine de caractère à une fenêtre :
// Handle = Handle de la fenêtre
// vString = Chaine de caractère
procedure TForm1.SendString (Handle : HWND; vString : String);
var
i: integer;
vKey: longint;
begin
SetforeGround(Handle); // Mise en avant plan de la fenêtre
if vString <> '' then
begin
i := 1;
While i <= length(vString) do
begin
vKey:= ord(vString[i]);
PostMessage(Handle, WM_CHAR, vKey, 0);
inc(i);
end;
end;
end;
// Envoi d'une touche système (ENTER par exemple)
procedure TForm1.SendStringKey (Handle : HWND; vKey : LongInt );
begin
SetforeGround(Handle);
PostMessage(Handle, WM_CHAR, vKey, 0);
end;
procedure TForm1.Envoi;
var
Handle : Hwnd;
begin
// Handle de la fenêtre Notepad :
Handle := FindWindow(Pchar('Notepad'),Pchar('Sans titre - Bloc-notes'));
// Handle du champ Edit de Notepad :
Handle := GetWindow(Handle, GW_CHILD); // Ou bien utiliser FindWindowEx si GetWindow ne retourne pas la bonne fenêtre
SendString(Handle,'ça marche !');
SendStringKey(Handle,VK_RETURN);
end; |
Partager