-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWinUtils.pas
453 lines (362 loc) · 14.8 KB
/
WinUtils.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
unit WinUtils;
// Copyright (c) 1996 Jorge Romero Gomez, Merchise.
interface
uses
Windows, Messages, SysUtils;
// Message Queue Inspection ------------------------------------------------------
function WindowHasMessageWaiting( WinHandle : HWND ) : boolean;
const // See QS_XXX flags in GetQueueStatus
QS_USERINPUT = QS_KEY or QS_MOUSEBUTTON or QS_HOTKEY;
QS_IMPORTANT = QS_USERINPUT or QS_POSTMESSAGE;
function ThreadHasInputWaiting : boolean;
function ThreadHasMessageWaiting : boolean;
function ThreadHasImportantMessageWaiting : boolean;
function ThreadHasTheseMessagesWaiting( Mask : integer ) : boolean; // Mask = QS_XXX flags
// Window searching -----------------------------------------------------------------
function GetWindowOfClass( const ClassName : string ) : HWND;
function GetWindowOfCaption( const Caption : string ) : HWND;
// Window info -----------------------------------------------------------------
function GetWindowCaption( WinHandle : HWND ) : string;
function GetWindowClass( WinHandle : HWND ) : string;
function GetWindowInstance( WinHandle : HWND ) : HMODULE;
function GetWindowID( WinHandle : HWND ) : integer;
// Window styles ----------------------------------------------------------------
procedure ExcludeWindowStyle( WinHandle : HWND; NewStyle : integer );
procedure IncludeWindowStyle( WinHandle : HWND; NewStyle : integer );
procedure SetWindowStyle( WinHandle : HWND; NewStyle : integer );
function GetWindowStyle( WinHandle : HWND ) : integer;
procedure ExcludeWindowExStyle( WinHandle : HWND; NewExStyle : integer );
procedure IncludeWindowExStyle( WinHandle : HWND; NewExStyle : integer );
procedure SetWindowExStyle( WinHandle : HWND; NewExStyle : integer );
function GetWindowExStyle( WinHandle : HWND ) : integer;
// Window list -----------------------------------------------------------------
function GetWindowOwner( WinHandle : HWND ) : HWND;
function GetFirstChild( WinHandle : HWND ) : HWND;
function GetFirstSibling( WinHandle : HWND ) : HWND;
function GetLastSibling( WinHandle : HWND ) : HWND;
function GetNextSibling( WinHandle : HWND ) : HWND;
function GetPrevSibling( WinHandle : HWND ) : HWND;
// Window Visuals -----------------------------------------------------------------
procedure SetWindowRedraw( WinHandle : HWND; Redraw : boolean ); // For compatibility with Windowsx.h
procedure LockWindowPainting( WinHandle : HWND; Locked : boolean ); // Use this ones instead
procedure UnlockPainting( WinHandle : HWND );
function IsMinimized( WinHandle : HWND ) : boolean;
function IsMaximized( WinHandle : HWND ) : boolean;
function IsRestored( WinHandle : HWND ) : boolean;
procedure MaximizeWindow( WinHandle : HWND );
procedure MinimizeWindow( WinHandle : HWND );
procedure RestoreWindow( WinHandle : HWND );
procedure ActivateWindow( WinHandle : HWND ); // Brings window to front, and gives it the focus
procedure ShowDontActivateWindow( WinHandle : HWND ); // Only make sure it's visible
procedure TopmostWindow( WinHandle : HWND ); // Make window topmost
procedure HideWindow( WinHandle : HWND );
procedure ShowWindow( WinHandle : HWND );
procedure UpdateWindowFrame( WinHandle : HWND ); // Repaint window borders & caption
procedure SetWindowSizeable( WinHandle : HWND; WinSizeable : boolean ); // Allow/Disable resizing
function WindowIsSizeable( WinHandle : HWND) : boolean;
function GetRealClientRect( WinHandle : HWND ) : TRect;
function WindowIsDropTarget( WinHandle : HWND) : boolean; // True if this window accepts files dropping
// Window subclassing -----------------------------------------------------------------
type
EWndProcCannotBeRestored = class( Exception );
function CurrentWndProc( WinHandle : HWND ) : pointer;
function SubclassWindow( WinHandle : HWND; NewWndProc : pointer ) : pointer; // For compatibility with Windowsx.h
//function SubclassWindow( WinHandle : integer; NewWndProc : pointer ) : pointer; // For compatibility with Windowsx.h
function ChangeWndProc( WinHandle : HWND; NewWndProc : pointer ) : integer;
function RestoreWndProc( WinHandle : HWND; Data : integer ) : pointer;
function PrevWndProc( Data : dword ) : pointer;
implementation
// Window Messages -----------------------------------------------------------------
function WindowHasMessageWaiting( WinHandle : HWND ) : boolean;
var
Msg : TMsg;
begin
Result := PeekMessage( Msg, WinHandle, 0, 0, PM_NOREMOVE );
end;
function ThreadHasInputWaiting : boolean;
begin
Result := GetInputState;
end;
function ThreadHasMessageWaiting : boolean;
begin
Result := GetQueueStatus( QS_ALLINPUT ) <> 0;
end;
function ThreadHasImportantMessageWaiting : boolean;
begin
Result := GetQueueStatus( QS_IMPORTANT ) <> 0;
end;
function ThreadHasTheseMessagesWaiting( Mask : integer ) : boolean;
begin
Result := GetQueueStatus( Mask ) <> 0;
end;
// Window info -----------------------------------------------------------------
function GetWindowCaption( WinHandle : HWND ) : string;
begin
SetLength( Result, MAX_PATH );
SetLength( Result, GetWindowText( WinHandle, pchar( Result ), length( Result ) ) );
end;
function GetWindowClass( WinHandle : HWND ) : string;
begin
SetLength( Result, MAX_PATH );
SetLength( Result, GetClassName( WinHandle, pchar( Result ), length( Result ) ) );
end;
function GetWindowStyle( WinHandle : HWND ) : integer;
begin
Result := GetWindowLong( WinHandle, GWL_STYLE );
end;
procedure SetWindowStyle( WinHandle : HWND; NewStyle : integer );
begin
SetWindowLong( WinHandle, GWL_STYLE, NewStyle );
end;
procedure IncludeWindowStyle( WinHandle : HWND; NewStyle : integer );
begin
NewStyle := GetWindowStyle( WinHandle ) or NewStyle;
SetWindowLong( WinHandle, GWL_STYLE, NewStyle );
end;
procedure ExcludeWindowStyle( WinHandle : HWND; NewStyle : integer );
begin
NewStyle := GetWindowStyle( WinHandle ) and (not NewStyle);
SetWindowLong( WinHandle, GWL_STYLE, NewStyle );
end;
function GetWindowExStyle( WinHandle : HWND ) : integer;
begin
Result := GetWindowLong( WinHandle, GWL_EXSTYLE );
end;
procedure SetWindowExStyle( WinHandle : HWND; NewExStyle : integer );
begin
SetWindowLong( WinHandle, GWL_EXSTYLE, NewExStyle );
end;
procedure IncludeWindowExStyle( WinHandle : HWND; NewExStyle : integer );
begin
NewExStyle := GetWindowExStyle( WinHandle ) or NewExStyle;
SetWindowLong( WinHandle, GWL_EXSTYLE, NewExStyle );
end;
procedure ExcludeWindowExStyle( WinHandle : HWND; NewExStyle : integer );
begin
NewExStyle := GetWindowExStyle( WinHandle ) and (not NewExStyle);
SetWindowLong( WinHandle, GWL_EXSTYLE, NewExStyle );
end;
function GetWindowOwner( WinHandle : HWND ) : HWND;
begin
Result := GetWindow( GW_OWNER, WinHandle );
end;
function GetWindowInstance( WinHandle : HWND ) : HMODULE;
begin
Result := GetWindowLong( WinHandle, GWL_HINSTANCE);
end;
function GetFirstChild( WinHandle : HWND ) : HWND;
begin
Result := GetTopWindow( WinHandle );
end;
function GetFirstSibling( WinHandle : HWND ) : HWND;
begin
Result := GetWindow( WinHandle, GW_HWNDFIRST );
end;
function GetLastSibling( WinHandle : HWND ) : HWND;
begin
Result := GetWindow( WinHandle, GW_HWNDLAST );
end;
function GetNextSibling( WinHandle : HWND ) : HWND;
begin
Result := GetWindow( WinHandle, GW_HWNDNEXT );
end;
function GetPrevSibling( WinHandle : HWND ) : HWND;
begin
Result := GetWindow( WinHandle, GW_HWNDPREV );
end;
function GetWindowID( WinHandle : HWND ) : integer;
begin
Result := GetDlgCtrlID( WinHandle );
end;
// Window searching -----------------------------------------------------------------
function GetWindowOfCaption( const Caption : string ) : HWND;
begin
Result := FindWindow( nil, pchar(Caption) );
end;
function GetWindowOfClass( const ClassName : string ) : HWND;
begin
Result := FindWindow( pchar(ClassName), nil );
end;
// Window Visuals -----------------------------------------------------------------
procedure SetWindowRedraw( WinHandle : HWND; Redraw : boolean );
begin
SendMessage( WinHandle, WM_SETREDRAW, WPARAM( Redraw ), 0 );
end;
procedure LockWindowPainting( WinHandle : HWND; Locked : boolean );
begin
SendMessage( WinHandle, WM_SETREDRAW, WPARAM( not Locked ), 0 );
end;
procedure UnlockPainting( WinHandle : HWND );
begin
LockWindowPainting( WinHandle, false );
InvalidateRect( WinHandle, nil, false );
end;
function IsMinimized( WinHandle : HWND ) : boolean;
begin
Result := IsIconic( WinHandle );
end;
function IsMaximized( WinHandle : HWND ) : boolean;
begin
Result := IsZoomed( WinHandle );
end;
function IsRestored( WinHandle : HWND ) : boolean;
begin
Result := GetWindowStyle( WinHandle ) and ( WS_MINIMIZE or WS_MAXIMIZE ) = 0;
end;
procedure MaximizeWindow( WinHandle : HWND );
begin
Windows.ShowWindow( WinHandle, SW_MAXIMIZE );
end;
procedure MinimizeWindow( WinHandle : HWND );
begin
Windows.ShowWindow( WinHandle, SW_MINIMIZE );
end;
procedure RestoreWindow( WinHandle : HWND );
begin
Windows.ShowWindow( WinHandle, SW_RESTORE );
end;
procedure HideWindow( WinHandle : HWND );
begin
Windows.ShowWindow( WinHandle, SW_HIDE );
end;
procedure ShowWindow( WinHandle : HWND );
begin
Windows.ShowWindow( WinHandle, SW_SHOWNORMAL );
end;
procedure ActivateWindow( WinHandle : HWND );
begin
Windows.ShowWindow( WinHandle, SW_SHOWNORMAL );
SetForegroundWindow( WinHandle );
end;
procedure TopmostWindow( WinHandle : HWND );
begin
SetWindowPos( WinHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOMOVE or SWP_NOACTIVATE );
end;
procedure ShowDontActivateWindow( WinHandle : HWND );
begin
SetWindowPos( WinHandle, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOMOVE or SWP_NOACTIVATE );
end;
procedure UpdateWindowFrame( WinHandle : HWND );
begin
SetWindowPos( WinHandle, 0, 0, 0, 0, 0, SWP_FRAMECHANGED or SWP_NOSIZE or SWP_NOMOVE or SWP_NOACTIVATE or SWP_NOZORDER );
end;
procedure SetWindowSizeable( WinHandle : HWND; WinSizeable : boolean );
begin
if WinSizeable
then IncludeWindowStyle( WinHandle, WS_SIZEBOX )
else ExcludeWindowStyle( WinHandle, WS_SIZEBOX );
UpdateWindowFrame( WinHandle );
end;
function WindowIsSizeable( WinHandle : HWND ) : boolean;
begin
Result := ( GetWindowStyle( WinHandle ) and WS_SIZEBOX ) <> 0;
end;
function GetRealClientRect( WinHandle : HWND ) : TRect;
var
WinStyle : dword;
begin
WinStyle := GetWindowStyle( WinHandle );
GetClientRect( WinHandle, Result );
with Result do
begin
if WinStyle and WS_HSCROLL <> 0
then inc( Bottom, GetSystemMetrics( SM_CYHSCROLL ) );
if WinStyle and WS_VSCROLL <> 0
then inc( Right, GetSystemMetrics( SM_CXVSCROLL ) );
end;
end;
function WindowIsDropTarget( WinHandle : HWND) : boolean;
begin
Result := ( GetWindowExStyle( WinHandle ) and WS_EX_ACCEPTFILES ) <> 0;
end;
function SubclassWindow( WinHandle : HWND; NewWndProc : pointer ) : pointer;
begin
Result := pointer( SetWindowLong( WinHandle, GWL_WNDPROC, longint(NewWndProc) ) );
end;
function CurrentWndProc( WinHandle : HWND ) : pointer;
begin
Result := pointer( GetWindowLong( WinHandle, GWL_WNDPROC ) );
end;
// -----------------------------------------------------------------
type
PDoubleLinkedNode = ^TDoubleLinkedNode;
TDoubleLinkedNode =
record
Prev, Next : PDoubleLinkedNode;
Data : integer;
end;
function NewNode( aPrev, aNext : pointer; aData : dword ) : PDoubleLinkedNode;
begin
new( Result );
with Result^ do
begin
Prev := aPrev;
Next := aNext;
Data := aData;
end;
end;
procedure FreeNode( Node : PDoubleLinkedNode );
begin
with Node^ do
begin // Chain Prev & Next
if Prev <> nil
then Prev.Next := Next;
if Next <> nil
then Next.Prev := Prev;
end;
Dispose( Node );
end;
function PrevWndProc( Data : dword ) : pointer;
var
Node : PDoubleLinkedNode absolute Data;
begin
Result := pointer( Node.Prev.Data )
end;
function ChangeWndProc( WinHandle : HWND; NewWndProc : pointer ) : integer;
var
ListAtom : pchar;
List : PDoubleLinkedNode;
Node : PDoubleLinkedNode absolute Result;
begin
ListAtom := pchar( GlobalAddAtom( 'WndProc_List' ) );
List := PDoubleLinkedNode( GetProp( WinHandle, ListAtom ) );
if List = nil
then
begin // Create list
List := NewNode( nil, nil, GetWindowLong( WinHandle, GWL_WNDPROC ) );
SetProp( WinHandle, ListAtom, dword( List ) );
end;
while List.Next <> nil do
List := List.Next;
Node := NewNode( List, nil, integer( NewWndProc ) );
List.Next := Node;
SubclassWindow( WinHandle, NewWndProc );
end;
function RestoreWndProc( WinHandle : HWND; Data : integer ) : pointer;
var
ListAtom : pchar;
List : PDoubleLinkedNode;
Node : PDoubleLinkedNode absolute Data;
begin
if Data <> 0
then
begin
ListAtom := pchar( GlobalAddAtom( 'WndProc_List' ) );
List := PDoubleLinkedNode( GetProp( WinHandle, ListAtom ) );
with Node^ do
begin
if Next = nil
then Result := SubclassWindow( WinHandle, pointer( Prev.Data ) )
else Result := CurrentWndProc( WinHandle );
if Prev = List // Free list
then
begin
FreeNode( List );
RemoveProp( WinHandle, ListAtom );
end;
end;
FreeNode( Node );
end
else Result := nil;
end;
end.