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 96 97 98 99 100 101 102
|
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls;
Type
Pwindows = ^Twindows;
Twindows = record
windowhandle : hwnd;
windowtext : string;
windowclass : string;
end;
type
TForm1 = class(TForm)
Button1: TButton;
TreeView1: TTreeView;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
Pnode, Cnode : TTreeNode;
AWindows : PWindows;
implementation
{$R *.dfm}
// recherche des objets ENFANT de la fenetre
function enumchildwindowsproc(wnd : hwnd;form : Tform1): bool; export;
{$ifdef win32} stdcall; {$endif}
var
buffer : array[0..99] of char;
nomclass : array[0..255] of char;
begin
result := true;
getclassname(wnd,nomclass,256);
getwindowtext(wnd,buffer,100);
new(awindows);
awindows^.windowhandle := wnd;
awindows^.windowtext := strpas(buffer);
awindows^.windowclass := strpas(nomclass);
Cnode := Form1.treeview1.items.AddChildObject(pnode, awindows^.windowtext ,awindows);
//relance des enfants suivant
if getwindow(wnd,GW_CHILD) = 0 then
begin
pnode := cnode;
enumchildwindows(wnd,@enumchildwindowsproc,0);
end;
end;
// recherche des objects PARENT
function enumwindowsproc(wnd : hwnd;form : Tform1): bool; export;
{$ifdef win32} stdcall; {$endif}
var
buffer : array[0..99] of char;
nomclass : array[0..255] of char;
begin
result := true;
getclassname(wnd,nomclass,256);
getwindowtext(wnd,buffer,100);
new(awindows);
awindows^.windowhandle := wnd;
awindows^.windowtext := strpas(buffer);
awindows^.windowclass := strpas(nomclass);
Pnode := Form1.treeview1.items.AddChildObject(nil, awindows^.windowtext ,awindows);
//relance des enfants suivant
enumchildwindows(wnd,@enumchildwindowsproc,0);
end;
//lancement du remplissage du Treeview
procedure TForm1.Button1Click(Sender: TObject);
begin
enumwindows(@enumwindowsproc,longint(self));
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
dispose(awindows);
end;
//evenement lors du click sur un noeud
//je veux juste afficher le texte dans un showmessage
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
showmessage (awindows(treeview1.selected.Data)^.windowtext);
end;
end. |
Partager