-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathenumerate-window-via-EnumWindows.pas
More file actions
86 lines (64 loc) · 1.64 KB
/
enumerate-window-via-EnumWindows.pas
File metadata and controls
86 lines (64 loc) · 1.64 KB
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
uses
System.SysUtils, Winapi.Windows, Generics.Collections;
// ...
type
TWindowEnumParam = class
private
FList : TDictionary<THandle, String>;
public
{@C}
constructor Create(const AList : TDictionary<THandle, String>);
{@G}
property List : TDictionary<THandle, String> read FList write FList;
end;
PWindowEnumParam = ^TWindowEnumParam;
constructor TWindowEnumParam.Create(const AList : TDictionary<THandle, String>);
begin
inherited Create();
///
FList := AList;
end;
// ...
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
begin
var ACaption := '';
var ACaptionLen := GetWindowTextLengthW(hWnd) + 1;
if ACaptionLen > 0 then begin
SetLength(ACaption, ACaptionLen);
GetWindowTextW(hWnd, PWideChar(ACaption), ACaptionLen);
end;
PWindowEnumParam(lParam).List.Add(hWnd, ACaption);
///
Result := True;
end;
function EnumerateWindows(var AList : TDictionary<THandle, String>) : Cardinal;
begin
result := 0;
///
if not Assigned(AList) then
AList := TDictionary<THandle, String>.Create()
else
AList.Clear();
var AWindowEnumParam := TWindowEnumParam.Create(AList);
if not EnumWindows(@EnumWindowsProc, NativeUInt(@AWindowEnumParam)) then
raise EWindowsException.Create('EnumWindows');
///
result := AList.Count;
end;
// ...
var AList := TDictionary<THandle, String>.Create();
try
EnumerateWindows(AList);
///
for var hWindow in AList.Keys do begin
var ACaption := '';
if not AList.TryGetValue(hWindow, ACaption) then
continue;
WriteLn(Format('(%d) %s', [
hWindow,
ACaption
]));
end;
finally
FreeAndNil(AList);
end;