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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Tlhelp32;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
function EnumWindowsCallback(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
implementation
{$R *.DFM}
procedure CenterWnd(const Wnd: HWND);
var
R: TRect;
SW, SH, W, H: Integer;
begin
SW := GetSystemMetrics(SM_CXSCREEN);
SH := GetSystemMetrics(SM_CYSCREEN);
GetWindowRect(Wnd, R);
W := R.Right - R.Left;
H := R.Bottom - R.Top;
R.Left := (SW - W) div 2;
if R.Left < 0 then R.Left := 0;
R.Top := (SH - H) div 2;
if R.Top < 0 then R.Top := 0;
MoveWindow(Wnd, R.Left, R.Top, W, H, True);
end;
Function GetProcessNameFromHandle(Handle:HWND):string;
var
Pid:DWord;
SnapShot:HWND;
Module:TModuleEntry32;
begin
Result:='';
if not IsWindow(Handle) then exit;
GetWindowThreadProcessId(Handle,@Pid);
Snapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,Pid);
try
if Snapshot<>-1 then
begin
Module.dwSize:=SizeOf(TModuleEntry32);
if Module32First(Snapshot,Module) then result:=Module.szExePath;
end;
finally
CloseHandle(Snapshot);
end;
end;
function EnumWindowsCallback(hWnd: HWND; lParam: LPARAM): BOOL;
Var Texte:array[0..250]of Char;
Classe:array[0..250]of Char;
begin
if pos(uppercase(form1.edit1.Text),uppercase(GetProcessNameFromHandle(hWnd)))>0 then
begin
CenterWnd(hWnd); //recentre la fenêtre
GetWindowText(hWnd,Texte,SizeOf(Texte)); // récupération du texte de la fenêtre
GetClassName(hWnd,Classe,SizeOf(Classe)); // récupération de la classe
Form1.Memo1.lines.add('classe :'+Classe+' Texte: '+Texte+' Handle: '+IntToStr(hWnd)
+' Exécutable : '+GetProcessNameFromHandle(hWnd));
end;
result:=true;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
EnumWindows(@EnumWindowsCallback,0);
showmessage('terminé');
end; |
Partager