-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunitWindowFinder.pas
317 lines (261 loc) · 7.08 KB
/
unitWindowFinder.pas
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
unit unitWindowFinder;
interface
uses
System.SysUtils, System.Classes, System.Generics.Collections, VCL.StdCtrls,
WinAPI.Windows;
type
TWindowRec = class(TObject)
protected
fExeName: string;
fWindowTitle: string;
fWindowClass: string;
fCurrentHandle: THandle;
public
constructor create(anExeName: string; aWinTitle: string; aWinClass: string; aHandle: THandle);
property exeName: string read fExeName;
property windowTitle: string read fWindowTitle;
property windowClass: string read fWindowClass;
property currentHandle: THandle read FcurrentHandle;
end;
TWindowFinder = class(TObject)
protected
fSubscribers: TList<TNotifyEvent>;
fWindows: TList<TWindowRec>;
function getCount: integer;
function getItem(index: integer): TWindowRec;
public
constructor create;
destructor Destroy; override;
procedure clear;
procedure loadWindowList;
procedure loadComboWithList(aComboBox: TCOmboBox);
procedure subscribe(aNotifyEvent: TNotifyEvent);
procedure unsubscribe(aNotifyEvent: TNotifyEvent);
function getWindow(anExeName: string; aWindowTitle: string; aWindowClass: string): TWindowRec;
function getWindowIndex(anExeName: string; aWindowTitle: string; aWindowClass: string): integer; overload;
function getWindowIndex(partialWindowTitle: string): integer; overload;
property count: integer read getCount;
property items[index: integer]: TWindowRec read getItem;
end;
var
windowFinder: TWindowFinder;
implementation
uses
TlHelp32;
{ TWindowFinder }
constructor TWindowFinder.create;
begin
inherited create;
fSubscribers := TList<TNotifyEvent>.create;
fWindows := TList<TWindowRec>.create;
end;
destructor TwindowFinder.destroy;
begin
clear;
fWindows.free;
fSubscribers.free;
inherited;
end;
function TWindowFinder.getCount: integer;
begin
result := fWindows.count;
end;
function TWindowFinder.getItem(index: integer): TWindowRec;
begin
result := fWindows[index];
end;
function TWindowFinder.getWindowIndex(partialWindowTitle: string): integer;
var
loop: integer;
temp: string;
begin
result := -1;
partialWindowTitle := upperCase(partialWindowTitle);
for loop := 0 to fWindows.count - 1 do
begin
temp := upperCase(fWindows[loop].windowTitle);
if (pos(partialWindowTitle, temp) > 0) then
begin
result := loop;
break;
end;
end;
end;
function TWindowFinder.getWindowIndex(anExeName, aWindowTitle,
aWindowClass: string): integer;
var
loop: integer;
begin
result := -1;
for loop := 0 to fWIndows.count - 1 do
begin
if (
( ((anExeName <> '') and (compareText(fWindows[loop].exeName, anExeName) = 0)) or (anExeName = '') ) and
( ((aWindowTitle <> '') and (compareText(fWindows[loop].windowTitle, aWindowTitle) = 0)) or (aWindowTitle = '') ) and
( ((aWindowClass <> '') and (compareText(fWindows[loop].windowClass, aWindowClass) = 0)) or (aWindowClass = '') )
) then
begin
result := loop; // fWindows[loop];
break;
end;
end;
end;
function TWindowFinder.getWindow(anExeName, aWindowTitle, aWindowClass: string): TWindowRec;
var
idx: integer;
begin
idx := getWindowIndex(anExeName, aWindowTitle, aWindowClass);
if (idx >= 0) then
begin
result := fWindows[idx];
end
else
begin
result := nil;
end;
end;
procedure TWindowFinder.loadComboWithList(aComboBox: TCOmboBox);
var
loop: integer;
begin
aComboBox.items.clear;
for loop := 0 to fWindows.count - 1 do
begin
aComboBox.items.addObject(format('[%s] - %s (Class %s)',
[fWindows[loop].exeName, fWindows[loop].windowTitle, fWindows[loop].windowClass]),
pointer(fWindows[loop].currentHandle));
end;
end;
procedure TWindowFinder.subscribe(aNotifyEvent: TNotifyEvent);
begin
if (fSubscribers.IndexOf(aNotifyEvent) < 0) then
begin
fSubscribers.add(aNotifyEvent);
end;
end;
procedure TWindowFinder.unsubscribe(aNotifyEvent: TNotifyEvent);
var
idxPos: integer;
begin
idxPos := fSubscribers.indexOf(aNotifyEvent);
if (idxPos >= 0) then
begin
fSubscribers.delete(idxPos);
end;
end;
function getWindowExeName(Handle: THandle): String;
var
PE: TProcessEntry32;
Snap: THandle;
ProcessId: cardinal;
begin
result := '';
pe.dwsize:=sizeof(PE);
GetWindowThreadProcessId(Handle,@ProcessId);
Snap := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if Snap <> 0 then
begin
if Process32First(Snap, PE) then
begin
if PE.th32ProcessID = ProcessId then
begin
Result:= String(PE.szExeFile)
end
else
begin
while Process32Next(Snap, PE) do
begin
if PE.th32ProcessID = ProcessId then
begin
Result:= String(PE.szExeFile);
break;
end;
end;
end;
end;
closeHandle(Snap);
end;
end;
function processWindow(wnd: Thandle; list: TStringList):boolean; stdcall;
var
temp:array[0..255] of char;
exeName: string;
winTitle: string;
winClass: string;
info:tagWINDOWINFO;
itemText: string;
idx: integer;
begin
getWindowInfo(wnd, info);
if ((info.dwStyle and WS_CHILD) = 0) and ((info.dwExStyle and WS_EX_TOOLWINDOW) = 0) then // and (isWindowVisible(wnd)) then
begin
getWindowText(wnd, temp, 256);
winTitle := PChar(@temp);
getClassNameW(wnd, temp, 256);
winClass := PChar(@temp);
exeName := getWindowExeName(wnd);
if (winTitle = '') then
begin
winTitle := '(untitled)';
end;
itemText := exename + '|' + winTitle + '|' +winClass;
idx := list.indexOf(itemText);
if (idx >= 0) then
begin
if (assigned(list.objects[idx])) then
begin
TWindowRec(list.objects[idx]).free;
list.objects[idx] := nil;
end;
end
else
begin
list.addObject(itemText, TWindowRec.create(exeName, winTitle, winClass, wnd));
end;
end;
result:=true;
end;
procedure TWindowFinder.clear;
var
loop: integer;
begin
for loop := 0 to fWIndows.count - 1 do
begin
fWindows[loop].free;
end;
fWindows.clear;
end;
procedure TWindowFinder.loadWindowList;
var
loop: integer;
temp: TStringList;
begin
temp := TStringList.create;
temp.caseSensitive := false;
clear;
enumDesktopwindows(getThreadDesktop(getCurrentThreadId), @processWindow, lparam(temp));
temp.sorted := true;
for loop := 0 to temp.count - 1 do
begin
if (assigned(temp.objects[loop])) then
begin
fWindows.add(TWindowRec(temp.objects[loop]));
end;
end;
temp.free;
for loop := 0 to fSubscribers.count - 1 do
begin
fSubscribers[loop](self);
end;
end;
{ TWindowRec }
constructor TWindowRec.create(anExeName, aWinTitle, aWinClass: string;
aHandle: THandle);
begin
inherited create;
fExeName := anExeName;
fWindowTitle := aWinTitle;
fWindowClass := aWinClass;
fCurrentHandle := aHandle;
end;
end.