/
liblayer.window.pas
425 lines (341 loc) · 9.28 KB
/
liblayer.window.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
unit libLayer.Window;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, JwaWindows,
libLayer.Bitmap, libLayer.PaintThread;
type
TLayerClickEvent = procedure(X, Y: Int32; var Block: Boolean); cdecl;
TLayer = class
private
FChildren: array of HWND;
FPaintInterval: Int32;
FPaintThread: TPaintThread;
FWindow: HWND;
FTarget: HWND;
FOffset: POINT;
FOnClick: TLayerClickEvent;
FBitmap: TLayerBitmap;
FWidth, FHeight: Int32;
FScriptThread: HANDLE;
function GetData: Pointer;
function GetScriptActive: Boolean;
procedure SetPaintInterval(Interval: Int32);
procedure SetScriptActive(Active: Boolean);
public
property Window: HWND read FWindow;
property Target: HWND read FTarget;
property OnClick: TLayerClickEvent read FOnClick write FOnClick;
property Width: Int32 read FWidth;
property Height: Int32 read FHeight;
property Data: Pointer read GetData;
property Bitmap: TLayerBitmap read FBitmap;
property PaintInterval: Int32 read FPaintInterval write SetPaintInterval;
property Offset: POINT read FOffset;
property ScriptActive: Boolean read GetScriptActive write SetScriptActive;
procedure AddChild(Handle: HWND);
function HasChild(Handle: HWND): Boolean;
procedure SetLayered;
procedure SetParent(Parent: HWND);
procedure SetTransparentColor(Color: Int32);
procedure SetBounds(Rect: RECT);
procedure Show;
procedure Hide;
procedure Close;
procedure BeginUpdate;
procedure EndUpdate;
procedure Paint;
constructor Create(ATarget: HWND; AScriptThread: UInt32);
destructor Destroy; override;
end;
PLayer = ^TLayer;
function WindowRect(Handle: HWND): RECT;
function GetLayerHandle(Handle: HWND): HWND;
function GetLayer(Handle: HWND): TLayer;
implementation
uses
fgl, syncobjs, DwmApi,
libLayer.Hooks;
type
TLayerList = class(specialize TFPGObjectList<TLayer>)
private
FCriticalSection: TCriticalSection;
public
function Add(const Item: TLayer): Integer;
procedure Remove(const Item: TLayer);
destructor Destroy; override;
constructor Create(AFreeObjects: Boolean = True);
end;
var
LayerList: TLayerList;
function TLayerList.Add(const Item: TLayer): Integer;
begin
FCriticalSection.Enter();
try
inherited Add(Item);
finally
FCriticalSection.Leave();
end;
end;
procedure TLayerList.Remove(const Item: TLayer);
begin
FCriticalSection.Enter();
try
inherited Remove(Item);
finally
FCriticalSection.Leave();
end;
end;
destructor TLayerList.Destroy;
begin
while (Count > 0) do
First.Free();
FCriticalSection.Free();
inherited Destroy();
end;
constructor TLayerList.Create(AFreeObjects: Boolean);
begin
FCriticalSection := TCriticalSection.Create();
inherited Create(AFreeObjects);
end;
function GetLayerHandle(Handle: HWND): HWND;
const
CLASS_NAME = 'TLayer';
CLASS_SIZE = 6;
var
Arr: array[0..CLASS_SIZE + 1] of Char;
begin
Handle := GetWindow(Handle, GW_ENABLEDPOPUP);
if (Handle <> 0) and (isWindow(Handle)) and (GetClassName(Handle, @Arr[0], Length(Arr)) = CLASS_SIZE) and (UTF8Encode(Arr) = CLASS_NAME) then
Exit(Handle);
Exit(0);
end;
function GetLayer(Handle: HWND): TLayer;
var
Ptr: Pointer;
begin
Handle := GetLayerHandle(Handle);
if (Handle > 0) then
begin
Ptr := Pointer(PtrUInt(GetWindowLongPtr(Handle, GWL_USERDATA)));
if (not IsBadReadPtr(Ptr, SizeOf(Pointer))) then // Make sure it's in our memory space
Exit(TLayer(Ptr));
end;
Exit(nil);
end;
function GetChildren(Handle: HWND; lParam: LPARAM): BOOL; stdcall;
begin
PLayer(PtrUInt(lParam))^.addChild(Handle);
Exit(True);
end;
function WindowRect(Handle: HWND): RECT;
begin
Result := Default(RECT);
if (Win32MajorVersion >= 6) and (DwmCompositionEnabled) and (GetAncestor(Handle, GA_ROOT) = Handle) then
DwmGetWindowAttribute(Handle, DWMWA_EXTENDED_FRAME_BOUNDS, @Result, SizeOf(Result))
else
GetWindowRect(Handle, Result);
end;
function WindowProc(Handle: HWND; Message: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
if (Message = WM_ACTIVATEAPP) and (not IsWindow(GetWindowLongPtr(Handle, GWL_HWNDPARENT))) then // seems to catch target close
PostMessage(Handle, WM_CLOSE, 0, 0);
if (Message = WM_SHOWWINDOW) and (lParam = SW_PARENTCLOSING) then
{ nothing }
else
Result := DefWindowProc(Handle, Message, wParam, lParam);
end;
function TLayer.GetData: Pointer;
begin
if (FBitmap = nil) then
Exit(nil)
else
Exit(FBitmap.Ptr);
end;
function TLayer.GetScriptActive: Boolean;
var
Code: DWord = 0;
begin
Result := (GetExitCodeThread(FScriptThread, Code)) and (Code = STILL_ACTIVE);
end;
procedure TLayer.SetScriptActive(Active: Boolean);
begin
if (not Active) and (FScriptThread > 0) then
begin
CloseHandle(FScriptThread);
FScriptThread := 0;
end;
end;
procedure TLayer.SetPaintInterval(Interval: Int32);
begin
if (FPaintInterval = Interval) then
Exit;
FPaintInterval := Interval;
if (FPaintThread <> nil) then
begin
FPaintThread.Terminate();
FPaintThread.WaitFor();
FPaintThread.Free();
FPaintThread := nil;
end;
if (FPaintInterval > 0) then
FPaintThread := TPaintThread.Create(FPaintInterval, @Paint);
end;
procedure TLayer.AddChild(Handle: HWND);
begin
SetLength(FChildren, Length(FChildren) + 1);
FChildren[High(FChildren)] := Handle;
end;
function TLayer.HasChild(Handle: HWND): Boolean;
var
i: Int32;
begin
for i := 0 to High(FChildren) do
if (FChildren[i] = Handle) then
Exit(True);
Exit(False);
end;
procedure TLayer.SetLayered;
begin
SetWindowLong(FWindow, GWL_EXSTYLE, WS_EX_LAYERED or WS_EX_TRANSPARENT);
end;
procedure TLayer.SetParent(Parent: HWND);
begin
SetWindowLong(FWindow, GWL_HWNDPARENT, Parent);
end;
procedure TLayer.SetTransparentColor(Color: Int32);
begin
SetLayeredWindowAttributes(FWindow, Color, 0, LWA_COLORKEY);
end;
procedure TLayer.SetBounds(Rect: RECT);
begin
SetWindowPos(FWindow, 0, Rect.Left, Rect.Top, 0, 0, SWP_NOSIZE or SWP_NOZORDER or SWP_NOACTIVATE);
end;
procedure TLayer.Show;
begin
ShowWindow(FWindow, SW_SHOWNOACTIVATE);
end;
procedure TLayer.Hide;
begin
ShowWindow(FWindow, SW_HIDE);
end;
procedure TLayer.Close;
begin
SendMessage(FWindow, WM_CLOSE, 0, 0);
end;
procedure TLayer.BeginUpdate;
begin
if (FPaintThread <> nil) then
FPaintThread.Paused := True;
end;
procedure TLayer.EndUpdate;
begin
if (FPaintThread <> nil) then
FPaintThread.Paused := False;
end;
procedure TLayer.Paint;
var
DC: HDC;
begin
if (FBitmap <> nil) then
begin
DC := GetWindowDC(FWindow);
BitBlt(DC, FOffset.X, FOffset.Y, FBitmap.Width, FBitmap.Height, FBitmap.DC, 0, 0, SRCCOPY);
ReleaseDC(FWindow, DC);
end;
end;
constructor TLayer.Create(ATarget: HWND; AScriptThread: UInt32);
procedure CreateWindow;
var
WindowClass: WNDCLASS;
R: RECT;
begin
with WindowClass do
begin
Style := 0;
lpfnWndProc := @WindowProc;
cbClsExtra := 0;
cbWndExtra := 0;
hInstance := System.HINSTANCE;
hIcon := 0;
hCursor := LoadCursor(0, IDC_ARROW);
hbrBackground := GetStockObject(BLACK_BRUSH);
lpszMenuName := nil;
lpszClassName := PChar(String(ClassName));
end;
RegisterClass(WindowClass);
R := WindowRect(FTarget);
FWidth := R.Right - R.Left;
FHeight := R.Bottom - R.Top;
FWindow := JwaWindows.CreateWindow(
PChar(String(ClassName)), PChar(String(ClassName)), WS_POPUP or WS_SYSMENU,
R.Left, R.Top, FWidth, FHeight, 0, 0, System.HINSTANCE, nil
);
SetWindowLongPtr(FWindow, GWL_USERDATA, PtrUInt(Self));
end;
var
Root: HWND;
begin
FTarget := ATarget;
FBitmap := nil;
FOnClick := nil;
FPaintThread := nil;
FPaintInterval := 0;
FWidth := 0;
FHeight := 0;
FOffset.X := 0;
FOffset.Y := 0;
Root := GetAncestor(FTarget, GA_ROOT);
if (Root <> FTarget) then // Target is a child window, so we will offset painting
begin
FOffset.X := WindowRect(FTarget).Left - WindowRect(Root).Left;
FOffset.Y := WindowRect(FTarget).Top - WindowRect(Root).Top;
FTarget := Root;
end;
// Store script thread
FScriptThread := OpenThread(THREAD_QUERY_INFORMATION, False, AScriptThread);
// Get children
SetLength(FChildren, 0);
EnumChildWindows(FTarget, @GetChildren, PtrUInt(@Self));
// Close old layer if needed
if (GetLayerHandle(FTarget) > 0) then
SendMessage(GetLayerHandle(FTarget), WM_CLOSE, 0, 0);
// Create new layer
CreateWindow();
// Setup layer window
Show();
SetLayered();
SetTransparentColor(0);
SetParent(FTarget);
// Focus the target window, else sometimes the layer wont show.
BringWindowToTop(FTarget);
// Add hooks
LayerHooks.IncRef();
// Create Image
FBitmap := TLayerBitmap.Create(FWidth, FHeight);
// Add to a list so we can free unfreed ones
LayerList.Add(Self);
end;
destructor TLayer.Destroy;
begin
Close();
ScriptActive := False;
LayerHooks.DecRef();
LayerList.Remove(Self);
if (FPaintThread <> nil) then
begin
FPaintThread.Terminate();
FPaintThread.WaitFor();
FPaintThread.Free();
end;
if (FBitmap <> nil) then
FBitmap.Free();
inherited Destroy();
end;
initialization
if (Win32MajorVersion >= 6) then
InitDwmLibrary();
LayerList := TLayerList.Create(False);
finalization
LayerList.Free();
end.