-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFutureWindows.pas
More file actions
1019 lines (893 loc) · 27.7 KB
/
FutureWindows.pas
File metadata and controls
1019 lines (893 loc) · 27.7 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
This software is distributed under the BSD license.
Copyright (c) 2011, Tomasz Maszkowski
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- The name of Tomasz Maszkowski may not be used to endorse or promote
products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
History:
2011-08-08 - [tm] initial thread-based version
2011-08-09 - [tm] refactored to non-threaded implementation
2011-08-11 - [tm] added support for anonymous methods and exception handling
TODO:
- support for ansi/unicode windows
Usage:
procedure test();
begin
TFutureWindows.Expect(MESSAGE_BOX_WINDOW_CLASS)
.ExecProc(
procedure (const AWindow: IWindow)
begin
AWindow.Text := 'Test';
end
)
.ExecCloseWindow();
MessageBox(0, 'testing future window', '', MB_OK);
end;
}
{.$define EnableLogging}
unit FutureWindows;
interface
uses
Windows,
Classes,
Controls,
SysUtils;
const
MESSAGE_BOX_WINDOW_CLASS = '#32770';
DEFAULT_WAIT_TIME_SECS = 5.0;
type
IWindow = interface
['{1756ADCA-62F2-4B2C-BF04-2A7FDC993A5D}']
function GetAsControl: TControl;
function GetHandle: HWND;
function GetHeight: Integer;
function GetParent: IWindow;
function GetProcessId: Cardinal;
function GetRect: TRect;
function GetText: string;
function GetTextLen: Integer;
function GetThreadId: Cardinal;
function GetWidth: Integer;
function GetWindowClass: string;
function IsEnabled: Boolean;
function IsUnicode: Boolean;
function IsVisible: Boolean;
function IsWindowValid: Boolean;
function PostMessage(Msg: UINT; wParam: WPARAM; lParam: LPARAM): Boolean;
function SendMessage(Msg: UINT; wParam: WPARAM; lParam: LPARAM): Integer;
procedure BringToFront;
procedure SetHeight(const Value: Integer);
procedure SetText(const Value: string);
procedure SetWidth(const Value: Integer);
property AsControl: TControl read GetAsControl;
property Enabled: Boolean read IsEnabled;
property Handle: HWND read GetHandle;
property Height: Integer read GetHeight write SetHeight;
property Parent: IWindow read GetParent;
property ProcessId: Cardinal read GetProcessId;
property Text: string read GetText write SetText;
property TextLen: Integer read GetTextLen;
property ThreadId: Cardinal read GetThreadId;
property Unicode: Boolean read IsUnicode;
property Valid: Boolean read IsWindowValid;
property Visible: Boolean read IsVisible;
property Width: Integer read GetWidth write SetWidth;
property WindowClass: string read GetWindowClass;
end;
IWindowAction = interface
['{8383E04C-F238-4505-94C2-B104A9467D0F}']
procedure Execute(const AWindow: IWindow);
end;
TProcessMessagesProc = procedure of object;
TWindowActionProc = reference to procedure(const AWindow: IWindow);
IExceptionHandler = interface
procedure HandleException(AExceptObject: TObject; AExceptAddr: Pointer);
end;
IFutureWindow = interface
function ExecAction(const AAction: IWindowAction): IFutureWindow;
function ExecProc(const AWindowActionProc: TWindowActionProc): IFutureWindow;
function ExecCloseWindow(): IFutureWindow;
function ExecPauseAction(ASeconds: Double; AProcessMessagesProc: TProcessMessagesProc): IFutureWindow;
function ExecSendKey(AKey: Word): IFutureWindow;
function GetDesciption: string;
function SetExceptionHandler(const AHandler: IExceptionHandler): IFutureWindow;
function TimedOut: Boolean;
function WindowFound: Boolean;
property Description: string read GetDesciption;
end;
TFutureWindows = class
class function Expect(const AWindowClass: string; AWaitSeconds: Double = DEFAULT_WAIT_TIME_SECS): IFutureWindow;
class function ExpectChild(AParent: HWND; const AWindowClass: string; AWaitSeconds: Double = DEFAULT_WAIT_TIME_SECS): IFutureWindow;
class function GetWindow(AHandle: THandle): IWindow;
end;
TWindowActions = class
class function CreateCloseWindowAction(): IWindowAction;
class function CreatePauseAction(ASeconds: Double; AProcessMessagesProc: TProcessMessagesProc): IWindowAction;
class function CreateProcAction(const AWindowActionProc: TWindowActionProc): IWindowAction;
class function CreateSendKeyAction(AKey: Word): IWindowAction;
end;
TAbstractWindowAction = class(TInterfacedObject, IWindowAction)
protected
procedure Execute(const AWindow: IWindow); virtual; abstract;
end;
implementation
uses
Messages;
type
PControl = ^TControl;
TWindow = class(TInterfacedObject, IWindow)
strict private
FWindowClass: string;
FHandle: HWND;
FProcessId: Cardinal;
FThreadId: Cardinal;
FControl: PControl;
FParent: IWindow;
procedure InitThreadIdAndProcessId;
procedure SetWindowSize(AWidth, AHeight: Integer);
private
function GetAsControl: TControl;
function GetHandle: HWND;
function GetHeight: Integer;
function GetParent: IWindow;
function GetProcessId: Cardinal;
function GetRect: TRect;
function GetText: string;
function GetTextLen: Integer;
function GetThreadId: Cardinal;
function GetWidth: Integer;
function GetWindowClass: string;
function IsEnabled: Boolean;
function IsUnicode: Boolean;
function IsVisible: Boolean;
function IsWindowValid: Boolean;
function PostMessage(Msg: UINT; wParam: WPARAM; lParam: LPARAM): Boolean;
function SendMessage(Msg: UINT; wParam: WPARAM; lParam: LPARAM): Integer;
procedure BringToFront;
procedure SetHeight(const Value: Integer);
procedure SetText(const Value: string);
procedure SetWidth(const Value: Integer);
public
constructor Create(AHandle: HWND);
destructor Destroy; override;
property AsControl: TControl read GetAsControl;
property Enabled: Boolean read IsEnabled;
property Handle: HWND read GetHandle;
property Parent: IWindow read GetParent;
property ProcessId: Cardinal read GetProcessId;
property Text: string read GetText write SetText;
property TextLen: Integer read GetTextLen;
property ThreadId: Cardinal read GetThreadId;
property Unicode: Boolean read IsUnicode;
property Valid: Boolean read IsWindowValid;
property Visible: Boolean read IsVisible;
property WindowClass: string read GetWindowClass;
property Width: Integer read GetWidth write SetWidth;
property Height: Integer read GetHeight write SetHeight;
end;
TAbstractFutureWindow = class(TInterfacedObject, IFutureWindow)
strict private
FActions: IInterfaceList;
FExecException: Exception;
FExecExceptAddr: Pointer;
FExceptionHandler: IExceptionHandler;
function GetAction(AIndex: Integer): IWindowAction;
function GetActionsCount: Integer;
procedure CheckRaiseExecException;
procedure TryCloseWindow(const AWindow: IWindow);
protected
function StartWaiting(): IFutureWindow; virtual; abstract;
procedure ExecuteActions(const AWindow: IWindow);
protected
{ IFutureWindow }
function ExecAction(const AAction: IWindowAction): IFutureWindow;
function ExecProc(const AWindowActionProc: TWindowActionProc): IFutureWindow;
function ExecCloseWindow(): IFutureWindow;
function ExecPauseAction(ASeconds: Double; AProcessMessagesProc: TProcessMessagesProc): IFutureWindow;
function ExecSendKey(AKey: Word): IFutureWindow;
function GetDesciption: string; virtual; abstract;
function SetExceptionHandler(const AHandler: IExceptionHandler): IFutureWindow;
function TimedOut: Boolean; virtual; abstract;
function WindowFound: Boolean; virtual; abstract;
public
destructor Destroy; override;
end;
//==============================================================================
// Non-threaded IFutureWindow implementation
TWindowCallback = procedure(const AWindow: IWindow) of object;
TWindowCallbackList = class
strict private type
PWindowCallback = ^TWindowCallback;
strict private
FItems: TList;
public
constructor Create;
destructor Destroy; override;
function Find(ACallback: TWindowCallback; out AIndex: Integer): Boolean;
function GetCount: Integer;
function GetItem(AIndex: Integer): TWindowCallback;
function Has(ACallback: TWindowCallback): Boolean;
procedure Add(ACallback: TWindowCallback);
procedure Clear;
procedure Delete(AIndex: Integer);
procedure Remove(ACallback: TWindowCallback);
public
property Count: Integer read GetCount;
property Items[AIndex: Integer]: TWindowCallback read GetItem; default;
end;
TFutureWindowObserver = class
strict private
class var FInstance: TFutureWindowObserver;
class function GetInstance: TFutureWindowObserver; static;
strict private
FCallbacks: TWindowCallbackList;
FCallbacksToRemove: TWindowCallbackList;
FCurrentProcessId: Cardinal;
FHandle: THandle;
FNotifying: Boolean;
constructor Create;
procedure CallCallbacks;
procedure CallCallbacksForWindow(const AWindow: IWindow);
procedure StartTimer;
procedure StopTimer;
procedure WndProc(var AMsg: TMessage);
public
destructor Destroy; override;
procedure RegisterCallback(ACallback: TWindowCallback);
procedure UnRegisterCallback(ACallback: TWindowCallback);
public
class procedure Finalize;
class property Instance: TFutureWindowObserver read GetInstance;
end;
TFutureWindow = class(TAbstractFutureWindow)
strict private
FParentWnd: HWND;
FTimedOut: Boolean;
FWaitSecs: Double;
FWindowClass: string;
FWindowFound: Boolean;
FStartedMillis: Cardinal;
private
{ IFutureWindowEx }
procedure CheckWindow(const AWindow: IWindow);
protected
function GetDesciption: string; override;
function StartWaiting(): IFutureWindow; override;
function TimedOut: Boolean; override;
function WindowFound: Boolean; override;
public
constructor Create(AParent: HWND; const AWindowClass: string; AWaitSecs: Double);
destructor Destroy; override;
end;
//===================== ACTIONS ================================================
TWindowActionProcAction = class(TAbstractWindowAction)
strict private
FProc: TWindowActionProc;
protected
procedure Execute(const AWindow: IWindow); override;
public
constructor Create(const AProc: TWindowActionProc);
end;
TSendKeyAction = class(TAbstractWindowAction)
strict private
FKey: Word;
protected
procedure Execute(const AWindow: IWindow); override;
public
constructor Create(AKey: Word);
end;
TCloseWindowAction = class(TAbstractWindowAction)
protected
procedure Execute(const AWindow: IWindow); override;
end;
TPauseAction = class(TAbstractWindowAction)
strict private
FSeconds: Double;
FProcessMessagesProc: TProcessMessagesProc;
protected
procedure Execute(const AWindow: IWindow); override;
public
constructor Create(ASeconds: Double; AProcessMessagesProc: TProcessMessagesProc);
end;
{$ifdef EnableLogging}
procedure log(const AMsg: string);
var
msg: string;
begin
msg := Format('[FutureWindows][%.4d] %s', [GetCurrentThreadId, AMsg]);
OutputDebugString(PChar(msg));
end;
{$endif}
{ TFutureWindows }
class function TFutureWindows.Expect(const AWindowClass: string;
AWaitSeconds: Double): IFutureWindow;
begin
Result := ExpectChild(0, AWindowClass, AWaitSeconds)
end;
class function TFutureWindows.ExpectChild(AParent: HWND;
const AWindowClass: string; AWaitSeconds: Double): IFutureWindow;
begin
Result := TFutureWindow.Create(AParent, AWindowClass, AWaitSeconds);
end;
class function TFutureWindows.GetWindow(AHandle: THandle): IWindow;
begin
Result := TWindow.Create(AHandle);
end;
{ TAbstractFutureWindow }
procedure TAbstractFutureWindow.CheckRaiseExecException;
var
e: Exception;
begin
if FExecException <> nil then
begin
e := FExecException;
FExecException := nil;
raise e at FExecExceptAddr;
end;
end;
destructor TAbstractFutureWindow.Destroy;
begin
FActions := nil;
inherited;
CheckRaiseExecException;
end;
function TAbstractFutureWindow.ExecAction(const AAction: IWindowAction): IFutureWindow;
begin
if FActions = nil then
FActions := TInterfaceList.Create;
FActions.Add(AAction);
Result := StartWaiting();
end;
function TAbstractFutureWindow.ExecProc(const AWindowActionProc: TWindowActionProc): IFutureWindow;
begin
Result := ExecAction(TWindowActionProcAction.Create(AWindowActionProc));
end;
function TAbstractFutureWindow.ExecCloseWindow: IFutureWindow;
begin
Result := ExecAction(TWindowActions.CreateCloseWindowAction());
end;
function TAbstractFutureWindow.ExecPauseAction(ASeconds: Double; AProcessMessagesProc: TProcessMessagesProc): IFutureWindow;
begin
Result := ExecAction(TWindowActions.CreatePauseAction(ASeconds, AProcessMessagesProc));
end;
function TAbstractFutureWindow.ExecSendKey(AKey: Word): IFutureWindow;
begin
Result := ExecAction(TWindowActions.CreateSendKeyAction(AKey))
end;
procedure TAbstractFutureWindow.ExecuteActions(const AWindow: IWindow);
var
i: Integer;
begin
Assert(AWindow <> nil);
Assert(AWindow.Valid);
{$ifdef EnableLogging}log('executing actions for: ' + GetDesciption);{$endif}
try
for i := 0 to GetActionsCount - 1 do
GetAction(i).Execute(AWindow);
except
if FExceptionHandler <> nil then
FExceptionHandler.HandleException(AcquireExceptionObject, ExceptAddr)
else
begin
FExecExceptAddr := ExceptAddr;
FExecException := AcquireExceptionObject;
end;
TryCloseWindow(AWindow);
end;
end;
function TAbstractFutureWindow.GetAction(AIndex: Integer): IWindowAction;
begin
Result := FActions[AIndex] as IWindowAction;
end;
function TAbstractFutureWindow.GetActionsCount: Integer;
begin
if FActions = nil then
Result := 0
else
Result := FActions.Count;
end;
function TAbstractFutureWindow.SetExceptionHandler(const AHandler: IExceptionHandler): IFutureWindow;
begin
FExceptionHandler := AHandler;
Result := Self;
end;
procedure TAbstractFutureWindow.TryCloseWindow(const AWindow: IWindow);
begin
TWindowActions.CreateCloseWindowAction().Execute(AWindow);
end;
{ TFutureWindowObserver }
procedure TFutureWindowObserver.CallCallbacks;
type
PEnumProcParams = ^TEnumProcParams;
TEnumProcParams = record
current_process_id: Cardinal;
windows_list: IInterfaceList;
end;
function enum_windows_proc(AHandle: HWND; AParam: LPARAM): Boolean; stdcall;
var
params: PEnumProcParams;
w: IWindow;
begin
w := TFutureWindows.GetWindow(AHandle);
params := PEnumProcParams(AParam);
if (w.ProcessId = params^.current_process_id) and
w.Visible and w.Enabled then
params^.windows_list.Add(w);
Result := True;
end;
var
i: Integer;
params: TEnumProcParams;
w: IWindow;
begin
if FNotifying or (FCallbacks.Count = 0) then Exit;
{$ifdef EnableLogging}log(ToString + '.CallCallbacks');{$endif}
FNotifying := True;
try
params.current_process_id := FCurrentProcessId;
params.windows_list := TInterfaceList.Create;
try
EnumWindows(@enum_windows_proc, Integer(@params));
for i := 0 to params.windows_list.Count - 1 do
begin
w := params.windows_list[i] as IWindow;
CallCallbacksForWindow(w);
end;
finally
params.windows_list := nil;
end;
finally
FNotifying := False;
end;
for i := 0 to FCallbacksToRemove.Count - 1 do
UnRegisterCallback(FCallbacksToRemove[i]);
FCallbacksToRemove.Clear;
end;
procedure TFutureWindowObserver.CallCallbacksForWindow(const AWindow: IWindow);
var
i: Integer;
cb: TWindowCallback;
begin
for i := FCallbacks.Count - 1 downto 0 do
begin
cb := FCallbacks[i];
if (not FCallbacksToRemove.Has(cb)) then
cb(AWindow)
end;
end;
constructor TFutureWindowObserver.Create;
begin
inherited Create;
FCurrentProcessId := GetCurrentProcessId;
FCallbacks := TWindowCallbackList.Create;
FCallbacksToRemove := TWindowCallbackList.Create;
end;
destructor TFutureWindowObserver.Destroy;
begin
StopTimer;
FCallbacks.Free;;
FCallbacksToRemove.Free;
inherited;
end;
class procedure TFutureWindowObserver.Finalize;
begin
FInstance.Free;
end;
class function TFutureWindowObserver.GetInstance: TFutureWindowObserver;
begin
if FInstance = nil then
FInstance := TFutureWindowObserver.Create;
Result := FInstance;
end;
procedure TFutureWindowObserver.RegisterCallback(ACallback: TWindowCallback);
begin
{$ifdef EnableLogging}log(ToString + '.RegisterCallback');{$endif}
Assert(not FCallbacks.Has(ACallback));
FCallbacks.Add(ACallback);
if FCallbacks.Count = 1 then
StartTimer;
end;
procedure TFutureWindowObserver.StartTimer;
begin
if FHandle = 0 then
begin
{$ifdef EnableLogging}log(ToString + '.StartTimer');{$endif}
FHandle := AllocateHWnd(WndProc);
Windows.SetTimer(FHandle, 1, 50, nil);
end;
end;
procedure TFutureWindowObserver.StopTimer;
begin
if FHandle <> 0 then
begin
{$ifdef EnableLogging}log(ToString + '.StopTimer');{$endif}
KillTimer(FHandle, 1);
DeallocateHWnd(FHandle);
FHandle := 0;
end;
end;
procedure TFutureWindowObserver.UnRegisterCallback(ACallback: TWindowCallback);
begin
{$ifdef EnableLogging}log(ToString + '.UnRegisterCallback');{$endif}
if FNotifying then
FCallbacksToRemove.Add(ACallback)
else
begin
FCallbacks.Remove(ACallback);
if FCallbacks.Count = 0 then
StopTimer;
end;
end;
procedure TFutureWindowObserver.WndProc(var AMsg: TMessage);
begin
case AMsg.Msg of
WM_TIMER:
CallCallbacks();
else
DefWindowProc(FHandle, AMsg.Msg, AMsg.WParam, AMsg.LParam)
end;
end;
{ TNonThreadedFutureWindow }
procedure TFutureWindow.CheckWindow(const AWindow: IWindow);
begin
Assert(not FWindowFound);
Assert(not FTimedOut);
FWindowFound := ((FParentWnd = 0) or (AWindow.Parent.Handle = FParentWnd)) and
(AWindow.WindowClass = FWindowClass);
if not FWindowFound then
FTimedOut := GetTickCount > (FStartedMillis + Round(FWaitSecs * 1000));
if FWindowFound or FTimedOut then
TFutureWindowObserver.Instance.UnRegisterCallback(CheckWindow);
if FWindowFound then
ExecuteActions(AWindow)
end;
constructor TFutureWindow.Create(AParent: HWND;
const AWindowClass: string; AWaitSecs: Double);
begin
inherited Create;
FParentWnd := AParent;
FWindowClass := AWindowClass;
FWaitSecs := AWaitSecs;
end;
destructor TFutureWindow.Destroy;
begin
{$ifdef EnableLogging}log(Self.ToString + '.Destroy');{$endif}
TFutureWindowObserver.Instance.UnRegisterCallback(CheckWindow);
// we might be raising en exception in inherited destructor so manually clean
// as much as possible
FWindowClass := '';
inherited;
end;
function TFutureWindow.GetDesciption: string;
begin
Result := FWindowClass;
end;
function TFutureWindow.StartWaiting: IFutureWindow;
begin
if FStartedMillis = 0 then
begin
{$ifdef EnableLogging}log(ToString + '.StartWaiting');{$endif}
FStartedMillis := GetTickCount;
TFutureWindowObserver.Instance.RegisterCallback(CheckWindow);
end;
Result := Self;
end;
function TFutureWindow.TimedOut: Boolean;
begin
Result := FTimedOut;
end;
function TFutureWindow.WindowFound: Boolean;
begin
Result := FWindowFound;
end;
{ TSendVKeyAction }
constructor TSendKeyAction.Create(AKey: Word);
begin
FKey := AKey
end;
procedure TSendKeyAction.Execute(const AWindow: IWindow);
begin
AWindow.PostMessage(WM_KEYDOWN, FKey, 0);
AWindow.PostMessage(WM_KEYUP, FKey, 0);
end;
{ TCloseWindowAction }
procedure TCloseWindowAction.Execute(const AWindow: IWindow);
begin
AWindow.SendMessage(WM_CLOSE, 0, 0);
end;
{ TPauseAction }
constructor TPauseAction.Create(ASeconds: Double; AProcessMessagesProc: TProcessMessagesProc);
begin
Assert(Assigned(AProcessMessagesProc));
FSeconds := ASeconds;
FProcessMessagesProc := AProcessMessagesProc;
end;
procedure TPauseAction.Execute(const AWindow: IWindow);
var
now, finish: Cardinal;
begin
if FSeconds = 0 then
FProcessMessagesProc()
else
begin
now := GetTickCount;
finish := now + Round(FSeconds * 1000);
while now < finish do
begin
FProcessMessagesProc();
now := GetTickCount;
end;
end;
end;
{ TWindow }
procedure TWindow.BringToFront;
begin
Windows.BringWindowToTop(FHandle)
end;
constructor TWindow.Create(AHandle: HWND);
begin
FHandle := AHandle;
end;
destructor TWindow.Destroy;
begin
Dispose(FControl);
inherited;
end;
function TWindow.GetAsControl: TControl;
begin
if FControl = nil then
begin
New(FControl);
FControl^ := FindControl(FHandle)
end;
Result := FControl^
end;
function TWindow.GetHandle: HWND;
begin
Result := FHandle
end;
function TWindow.GetHeight: Integer;
var
r: TRect;
begin
r := GetRect;
Result := r.Bottom - r.Top
end;
function TWindow.GetParent: IWindow;
begin
if FParent = nil then
FParent := TFutureWindows.GetWindow(Windows.GetParent(FHandle));
Result := FParent;
end;
function TWindow.GetProcessId: Cardinal;
begin
InitThreadIdAndProcessId;
Result := FProcessId;
end;
function TWindow.GetRect: TRect;
begin
if not Windows.GetWindowRect(FHandle, Result) then
Result := Rect(0, 0, 0, 0);
end;
function TWindow.GetText: string;
var
len: Integer;
begin
len := TextLen;
SetString(Result, PChar(nil), len);
if len > 0 then
SendMessage(WM_GETTEXT, len + 1, Integer(PChar(Result)))
end;
function TWindow.GetTextLen: Integer;
begin
Result := SendMessage(WM_GETTEXTLENGTH, 0, 0)
end;
function TWindow.GetThreadId: Cardinal;
begin
InitThreadIdAndProcessId;
Result := FThreadId;
end;
function TWindow.GetWidth: Integer;
var
r: TRect;
begin
r := GetRect;
Result := r.Right - r.Left
end;
function TWindow.GetWindowClass: string;
var
buffer: array[Byte] of Char;
begin
if FWindowClass = '' then
begin
Windows.GetClassName(FHandle, @buffer, Length(buffer));
FWindowClass := buffer;
end;
Result := FWindowClass;
end;
procedure TWindow.InitThreadIdAndProcessId;
begin
if FThreadId = 0 then
FThreadId := Windows.GetWindowThreadProcessId(FHandle, FProcessId)
end;
function TWindow.IsEnabled: Boolean;
begin
Result := Windows.IsWindowEnabled(FHandle)
end;
function TWindow.IsUnicode: Boolean;
begin
Result := Windows.IsWindowUnicode(FHandle)
end;
function TWindow.IsVisible: Boolean;
begin
Result := Windows.IsWindowVisible(FHandle)
end;
function TWindow.IsWindowValid: Boolean;
begin
Result := Windows.IsWindow(FHandle);
end;
function TWindow.PostMessage(Msg: UINT; wParam: WPARAM;
lParam: LPARAM): Boolean;
begin
if IsUnicode then
Result := Windows.PostMessageW(FHandle, Msg, wParam, lParam)
else
Result := Windows.PostMessageA(FHandle, Msg, wParam, lParam)
end;
function TWindow.SendMessage(Msg: UINT; wParam: WPARAM;
lParam: LPARAM): Integer;
begin
if IsUnicode then
Result := Windows.SendMessageW(FHandle, Msg, wParam, lParam)
else
Result := Windows.SendMessageA(FHandle, Msg, wParam, lParam)
end;
procedure TWindow.SetHeight(const Value: Integer);
begin
SetWindowSize(Width, Value);
end;
procedure TWindow.SetText(const Value: string);
begin
SendMessage(WM_SETTEXT, 0, Integer(PChar(Value)));
end;
procedure TWindow.SetWidth(const Value: Integer);
begin
SetWindowSize(Value, Height);
end;
procedure TWindow.SetWindowSize(AWidth, AHeight: Integer);
begin
Windows.SetWindowPos(FHandle, 0, 0, 0,
AWidth, AHeight,
SWP_NOZORDER or
SWP_NOMOVE or
SWP_NOACTIVATE
)
end;
{ TWindowCallbackList }
procedure TWindowCallbackList.Add(ACallback: TWindowCallback);
var
p: PWindowCallback;
begin
if FItems = nil then
FItems := TList.Create;
New(p);
p^ := ACallback;
FItems.Add(p);
end;
procedure TWindowCallbackList.Clear;
var
cnt: Integer;
begin
cnt := GetCount;
while cnt > 0 do
begin
Dec(cnt);
Delete(cnt);
end;
end;
constructor TWindowCallbackList.Create;
begin
FItems := TList.Create;
end;
procedure TWindowCallbackList.Delete(AIndex: Integer);
var
p: PWindowCallback;
begin
p := FItems[AIndex];
Dispose(p);
FItems.Delete(AIndex);
end;
destructor TWindowCallbackList.Destroy;
begin
Clear;
FItems.Free;
inherited;
end;
function TWindowCallbackList.Find(ACallback: TWindowCallback;
out AIndex: Integer): Boolean;
var
p: PWindowCallback;
method: TMethod;
begin
AIndex := 0;
method := TMethod(ACallback);
while AIndex < GetCount do
begin
p := FItems[AIndex];
if CompareMem(p, @method, SizeOf(TWindowCallback)) then
Exit(True);
Inc(AIndex);
end;
AIndex := -1;
Result := False;
end;
function TWindowCallbackList.GetCount: Integer;
begin
Result := FItems.Count;
end;
function TWindowCallbackList.GetItem(AIndex: Integer): TWindowCallback;
begin
Result := PWindowCallback(FItems[AIndex])^
end;
function TWindowCallbackList.Has(ACallback: TWindowCallback): Boolean;
var
dummy: Integer;
begin
Result := Find(ACallback, dummy)
end;
procedure TWindowCallbackList.Remove(ACallback: TWindowCallback);
var
idx: Integer;
begin
if Find(ACallback, idx) then
Delete(idx);
end;
{ TWindowActionProcAction }
constructor TWindowActionProcAction.Create(const AProc: TWindowActionProc);
begin
FProc := AProc;
end;
procedure TWindowActionProcAction.Execute(const AWindow: IWindow);
begin
FProc(AWindow);
end;
{ TWindowActions }
class function TWindowActions.CreateCloseWindowAction: IWindowAction;
begin
Result := TCloseWindowAction.Create
end;
class function TWindowActions.CreatePauseAction(ASeconds: Double;
AProcessMessagesProc: TProcessMessagesProc): IWindowAction;