Author: Jonas Bilinkevicius
How to get a list of all windows on the Desktop and their handles
Answer:
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 StdCtrls;
8
9 type
10 TForm1 = class(TForm)
11 Button1: TButton;
12 Memo1: TMemo;
13 procedure Button1Click(Sender: TObject);
14 procedure GetWins;
15 private
16 { Private declarations }
17 public
18 { Public declarations }
19 end;
20
21 var
22 Form1: TForm1;
23 WindowList: TList;
24
25 implementation
26
27 {$R *.DFM}
28
29 function GetWindows(Handle: HWND; Info: Pointer): BOOL; stdcall;
30 begin
31 Result := True;
32 WindowList.Add(Pointer(Handle));
33 end;
34
35 procedure TForm1.GetWins;
36 var
37 TopWindow, CurrentWindow: HWND;
38 Dest: array[0..80] of char;
39 ClassName: array[0..80] of char;
40 i: Integer;
41 begin
42 try
43 WindowList := TList.Create;
44 TopWindow := Handle;
45 EnumWindows(@GetWindows, Longint(@TopWindow));
46 CurrentWindow := TopWindow;
47 for i := 0 to WindowList.Count - 1 do
48 begin
49 CurrentWindow := GetNextWindow(CurrentWindow, GW_HWNDNEXT);
50 GetWindowText(CurrentWindow, Dest, sizeof(Dest) - 1);
51 GetClassName(CurrentWindow, ClassName, sizeof(ClassName) - 1);
52 if StrLen(Dest) > 0 then
53 Memo1.Lines.Add(Dest + ' = ' + ClassName + ' - ' + IntToStr(CurrentWindow));
54 end;
55 finally
56 WindowList.Free;
57 end;
58 end;
59
60 procedure TForm1.Button1Click(Sender: TObject);
61 begin
62 GetWins;
63 end;
64
65 end.
|