-
Notifications
You must be signed in to change notification settings - Fork 2
/
PJWdwState.pas
2049 lines (1930 loc) · 81.1 KB
/
PJWdwState.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
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 Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 1999-2014, Peter Johnson (www.delphidabbler.com).
*
* DelphiDabbler Window state components.
}
unit PJWdwState;
// Conditional defines
// Note: There is no version checking for Delphi 1 and 2 not since this unit
// will not compile with those compilers.
{$DEFINE WarnDirs} // $WARN compiler directives available
{$DEFINE RegAccessFlags} // TRegistry access flags available
{$DEFINE RequiresFileCtrl} // FileCtrl unit is required for ForceDirectories
{$UNDEF RTLNameSpaces} // Don't qualify RTL units names with namespaces
{$UNDEF TScrollStyleMoved} // TScrollStyle hasn't moved to System.UITypes units
{$UNDEF SupportsPathDelim} // PathDelim and related routine not defined
{$IFDEF VER100} // Delphi 3
{$UNDEF WarnDirs}
{$UNDEF RegAccessFlags}
{$ENDIF}
{$IFDEF VER120} // Delphi 4
{$UNDEF WarnDirs}
{$UNDEF RegAccessFlags}
{$ENDIF}
{$IFDEF VER130} // Delphi 5
{$UNDEF WarnDirs}
{$UNDEF RegAccessFlags}
{$ENDIF}
{$IFDEF CONDITIONALEXPRESSIONS}
{$IF CompilerVersion >= 24.0} // Delphi XE3 and later
{$LEGACYIFEND ON} // NOTE: this must come before all $IFEND directives
{$DEFINE TScrollStyleMoved}
{$IFEND}
{$IF CompilerVersion >= 23.0} // Delphi XE2 and later
{$DEFINE RTLNameSpaces}
{$IFEND}
{$IF CompilerVersion >= 14.0} // Delphi 6 and later
{$DEFINE SupportsPathDelim}
{$UNDEF WarnDirs}
{$UNDEF RequiresFileCtrl}
{$IFEND}
{$ENDIF}
interface
uses
// Delphi
{$IFDEF RTLNameSpaces}
System.Classes, Vcl.Controls, Winapi.Messages, Winapi.Windows, Vcl.Forms,
System.SysUtils, System.Win.Registry;
{$ELSE}
Classes, Controls, Messages, Windows, Forms, SysUtils, Registry
{$IFDEF RequiresFileCtrl}
, FileCtrl // needed for ForceDirectories since it's not in SysUtils yet.
{$ENDIF}
;
{$ENDIF}
const
// Custom messages used internally
// instructs component to set window state (normal, minimized or maximized)
PJM_SETWDWSTATE = WM_USER + 0;
// instructs MDI child components they can restore their windows
PJM_RESTOREMDICHILD = WM_USER + 1;
type
TPJCustomWdwState = class;
{
TPJWdwStateHook:
Class that hooks into window of owning form and passes on WMCreate and
WMDestroy messages to TPJCustomWdwState component. Instances of this class
should not be created by user code - this control is designed for private
use by TPJCustomWdwState.
}
TPJWdwStateHook = class(TWinControl)
private
fWdwState: TPJCustomWdwState;
{Reference to owning window state component}
procedure SendMsgToOwner(var Msg: TMessage);
{Dispatches given message to component's owner component.
@param Msg [in/out] The message. May be changed by message handler.
}
procedure WMDestroy(var Msg: TMessage); message WM_DESTROY;
{Handles WM_DESTROY message and dispatches it to owning window state
component.
@param Msg [in/out] The message. May be modified by message handler.
}
procedure CMShowingChanged(var Msg: TMessage); message CM_SHOWINGCHANGED;
{Handles CM_SHOWINGCHANGED message and dispatches it to owning window
state component.
@param Msg [in/out] The message. May be modified by message handler.
}
procedure PJMSetWindowState(var Msg: TMessage); message PJM_SETWDWSTATE;
{Handles PJM_SETWDWSTATE message and dispatches it to owning window state
component.
@param Msg [in/out] The message. May be modified by message handler.
}
public
constructor Create(AOwner: TComponent); override;
{Class constructor. Records reference to owner.
@param AOwner [in] Owning component. Must be a TPJCustomWdwState.
}
end;
{
EPJCustomWdwState:
Type of exception raised by TPJCustomWdwState.
}
EPJCustomWdwState = class(Exception);
{
TPJWdwStateReadEvent:
Type of event triggered after the window's stored placement information is
read from storage. Event handlers can adjust the values before they are used
to restore the window.
@param Sender [in] Reference to component triggering the event.
@param Left [in/out] Left edge of window. In: value read from storage.
Out: value modified in event handler. Leave unchanged or set to MaxInt
to use default value.
@param Top [in/out] Top edge of window. In: value read from storage. Out:
value modified in event handler. Leave unchanged or set to MaxInt to use
default value.
@param Width [in/out] Width of window. In: value read from storage. Out:
value modified in event handler. Leave unchanged or set to MaxInt to use
default value.
@param Height [in/out] Height of window. In: value read from storage. Out:
value modified in event handler. Leave unchanged or set to MaxInt to use
default value.
@param State [in/out] State of window. In: value read from storage. Out:
value modified in event handler. This value is the ordinal value of a
TWindowState value. Leave unchanged or set to MaxInt to use default
value.
}
TPJWdwStateReadEvent = procedure(Sender: TObject; var Left, Top, Width,
Height, State: Integer) of object;
{
TPJWdwStateOptions
Set of values that are stored in the component's Options property.
}
TPJWdwStateOptions = set of (
woIgnoreState, // stored wdw state is ignored and wdw is display normal
woIgnoreSize, // stored wdw size is ignored and wdw's defaults are used
woFitWorkArea // restored wdw appears wholy within work area
// (wdw may be resized to fit if woIgnoreSize not set)
// (work area is desktop or MDI client area for MDI child
// windows)
);
{
TPJCustomWdwState:
Abstract base class for components that record window size, position and
state between program executions.
}
TPJCustomWdwState = class(TComponent)
private
fAutoSaveRestore: Boolean;
{Value of AutoSaveRestore property}
fMinimizeDelay: Integer;
{Value of MinimizeDelay property}
fOnReadWdwState: TPJWdwStateReadEvent;
{Event handler for OnReadWdwState event}
fOnAfterWindowSized: TNotifyEvent;
{Event handler for OnAfterWindowSized event}
fOnAfterWindowRestored: TNotifyEvent;
{Event handler for OnAfterWindowRestored event}
fOptions: TPJWdwStateOptions;
{Value of Options property}
fHook: TPJWdwStateHook;
{Instance of a privately owned windowed control that is used to intercept
relevant messages in owning form and custom messages posted by this
component and to notify this component of the messages}
fWindow: TForm;
{Instance of form on which to operate}
fFormShown: Boolean;
{Flag false until form has been shown and true afterwards. Any call to
Restore method while this flag is false is recorded as pending and called
again once form has been shown}
fFormRestored: Boolean;
{Flag false until component has fully restored its window}
fRestorePending: Boolean;
{Flag true if Restore method has been called while fFormShown flag false:
such calls set this flag which in turn causes Restore method to be called
again once the form has been shown}
function GetIgnoreState: Boolean;
{Read accessor for IgnoreState property. Checks for presence of
woIgnoreState in Options property.
@return True if Options contain woIgnoreState, False otherwise.
}
procedure SetIgnoreState(const Value: Boolean);
{Write accessor for IgnoreState property. Includes or excludes
woIgnoreState property in Options according to Value.
@param Value [in] New property value.
}
protected
function GetMDIParentForm: TForm;
{Finds an MDI child form's parent form.
@return Reference to parent form. Nil if parent not found or if
component's form is not an MDI child form.
}
function GetWdwStateCmp(const Form: TForm): TPJCustomWdwState;
{Finds any TPJCustomWdwState component placed on a form.
@param Form [in] Form to be searched.
@return Reference to window state component or nil if no such component
on form.
}
function CanRestoreMDIChild: Boolean;
{Checks if an MDI child form can be restored.
@return True if form can be restored, False otherwise.
}
procedure WMDestroy(var Msg: TMessage); message WM_DESTROY;
{Message handler for owning form's WM_DESTROY message. The message is sent
to this component by the hook window component. Save the form's state if
the AutoSaveRestore property is true.
@param Msg [in/out] Not used.
}
procedure CMShowingChanged(var Msg: TMessage); message CM_SHOWINGCHANGED;
{Message handler for owning form's CM_SHOWINGCHANGED message. The message
ismsent to this component by the hook window component to indicate that
the form's showing state has changed. This causes any pending window
restoration to be executed.
@param Msg [in/out] Not used.
}
procedure PJMSetWindowState(var Msg: TMessage); message PJM_SETWDWSTATE;
{Message handler for owning form's custom PJM_SETWDWSTATE message. The
message is sent to this component by the hook window component. We update
the form's window state. A message is used for this purpose since the
Restore method needs to ensure the form has been shown before we execute
this code. Posting this message enables this to happen.
@param Msg [in/out] Message structure containing the required window
state in its LParam field. Message is not modified.
}
procedure PJMRestoreMDIChild(var Msg: TMessage);
message PJM_RESTOREMDICHILD;
{Message handled for custom PJM_RESTOREMDICHILD message. This message is
dispatched by a MDI parent form to all MDI child forms when the parent
form has restored. In response to the message we restore the form if the
AutoSaveRestore property is true and form has not been restored.
@param Msg [in/out] Not used.
}
procedure DispatchMDIChildMessages;
{Dispatches PJM_RESTOREMDICHILD messages to window state components on MDI
child forms.
}
procedure ReadWdwState(var Left, Top, Width, Height, State: Integer);
virtual; abstract;
{Read state of window from storage. Caller passes default values and
method returns new values from storage, or defaults if no stored values
found. Implementation depends on method of storage used by derived
classes.
@param Left [in/out] Left side of window. Default passed in. Value read
from storage passed out, or default if no value read.
@param Top [in/out] Top size of window. Default passed in. Value read
from storage passed out, or default if no value read.
@param Width [in/out] Width of window. Default width passed in. Value
read from storage passed out, or default if no value read.
@param Height [in/out] Height of window. Default height passed in. Value
read from storage passed out, or default if no value read.
@param State [in/out] Code describing state of window. Default state
passed in. Value read from storage passed out, or default if no value
read. This value is the ordinal value of a TWindowState value.
}
procedure DoReadWdwState(var Left, Top, Width, Height, State: Integer);
virtual;
{Fetches the state of the window from storage via the abstract
ReadWdwState method then triggers the OnReadWdwState event which permits
any of the values read to be modified before passing them back to the
caller. The default window values per the window's form properties are
passed into the method.
@param Left [in/out] Left side of window. Default passed in. Value read
from storage passed out, or default if no value read.
@param Top [in/out] Top size of window. Default passed in. Value read
from storage passed out, or default if no value read.
@param Width [in/out] Width of window. Default width passed in. Value
read from storage passed out, or default if no value read.
@param Height [in/out] Height of window. Default height passed in. Value
read from storage passed out, or default if no value read.
@param State [in/out] Code describing state of window. Default state
passed in. Value read from storage passed out, or default if no value
read. This value is the ordinal value of a TWindowState value.
}
procedure SaveWdwState(const Left, Top, Width, Height, State: Integer);
virtual; abstract;
{Save state of window to storage. Implementation depends on method of
storage used by derived classes.
@param Left [in] Left side of window.
@param Top [in] Top side of window.
@param Width [in] Width of window.
@param Height [in] Height of window.
@param State [in] Code representing state of window. This is the ordinal
value of a TWindowState value.
}
procedure SetParentComponent(Value: TComponent); override;
{Override of SetParentComponent method. Sets the parent of the hook
window. This enables the hook window to receive messages from the parent
window. The method normally called by the streaming system when component
is loaded on a form. We have made method public so that it can be called
explicitly when component is dynamically created.
@param Value [in] Reference to parent component.
}
property OnReadWdwState: TPJWdwStateReadEvent
read fOnReadWdwState write fOnReadWdwState;
{Event triggered just after the window's state is read from storage. Any
of the values read can be altered before the component sets the window's
properties. Setting any of the values to MaxInt causes the form's default
value to be used in place of the stored value}
public
constructor Create(AOwner: TComponent); override;
{Class constructor. Records reference to any owning form, sets default
property values and creates a hook window to trap messages from owning
form. Permits only one TPJCustomWdwState derived component to be placed on
the form.
NOTE: This constructor is only suitable for components present at design
time. When constructing components dynamically use the CreateStandAlone
constructor instead.
@param AOwner [in] Owning component. Must be a TForm.
@except EPJCustomWdwState raised if Owner is not a TForm.
@except EPJCustomWdwState raised if there is already a TPJCustomWdwState
component on the form.
}
constructor CreateStandAlone(AOwner: TForm); virtual;
{Class constructor. Creates instance of component dynamically, ensuring
all required housekeeping is performed. Use when constructing a component
that is not present at design time. AOwner must be a TForm.
@param AOwner [in] Owning component. Must be a TForm.
@except EPJCustomWdwState raised if Owner is not a TForm.
@except EPJCustomWdwState raised if there is already a TPJCustomWdwState
component on the form.
}
procedure Restore;
{Reads window placement and state from storage and set up the window's
size, position and state as required.
}
procedure Save;
{Save window placement, size and state to storage.
}
published
property AutoSaveRestore: Boolean
read fAutoSaveRestore write fAutoSaveRestore default False;
{When true component automatically restores window state on form creation
and saves it on form destruction}
property IgnoreState: Boolean
read GetIgnoreState write SetIgnoreState default False;
{When true Restore method ignores the saved state of the window and leaves
current state unchanged while still setting size and position. When false
Restore also sets the window state according to the saved state. Changing
this property updates the Options property: including and excluding
woIgnoreState in the set as necessary.
NOTE: Use of IgnoreState is now deprecated and Options should be used
instead}
property MinimizeDelay: Integer
read fMinimizeDelay write fMinimizeDelay default 100;
{When a form is to be started minimized this property determines the delay
(in ms) between displaying the normalised form on screen and minimising
it}
property Options: TPJWdwStateOptions
read fOptions write fOptions default [];
{Provides a set of display options that affect how the window is displayed
or if certain stored values are ignored. See the TPJWdwStateOptions type
definition for details. Including/excluding the woIgnoreState value is the
same as setting IgnoreState to true or false respectively}
property OnAfterWindowSized: TNotifyEvent
read fOnAfterWindowSized write fOnAfterWindowSized;
{Event triggered immediately after window has been sized, but before it
has been restored}
property OnAfterWindowRestored: TNotifyEvent
read fOnAfterWindowRestored write fOnAfterWindowRestored;
{Event triggered immediately after window has been restored to required
state}
end;
{
TPJWdwStateData:
Record used to store window state information.
}
TPJWdwStateData = record
Left: Integer; // position of left side of window
Top: Integer; // position of top of window
Width: Integer; // width of window
Height: Integer; // height of window
State: Integer; // state of window (ordinal value of TWindowState value)
end;
{
TPJWdwStateReadData:
Type of event triggered by TPJUserWdwState when window state data is
to be read from persistent storage.
@param Sender [in] Reference to component triggering this event.
@param Data [in/out] Window state informatiom. Set to default values when
called. Handler should set Data fields to values it reads from
persistent storage.
}
TPJWdwStateReadData = procedure(Sender: TObject; var Data: TPJWdwStateData)
of object;
{
TPJWdwStateSaveData:
Type of event triggered by TPJUserWdwState when window state data is
to be written to persistent storage.
@param Sender [in] Reference to component triggering this event.
@param Data [in] Window state data to be written to persistent storage.
}
TPJWdwStateSaveData = procedure(Sender: TObject; const Data: TPJWdwStateData)
of object;
{
TPJUserWdwState:
Implements a component that records a window's size, position and state
between program executions in persistent storage. The user must provide the
mechanism for storing and saving by handling the OnReadData and OnSaveData
events.
}
TPJUserWdwState = class(TPJCustomWdwState)
private
fOnReadData: TPJWdwStateReadData;
{Event handler for OnReadData event}
fOnSaveData: TPJWdwStateSaveData;
{Event handler for OnSaveData event}
protected
procedure ReadWdwState(var Left, Top, Width, Height, State: Integer);
override;
{Gets window state information from OnReadData event. If no event handler
is assigned default window state is used.
@param Left [in/out] Left side of window. Default passed in. Value read
from storage passed out, or default if no value read.
@param Top [in/out] Top size of window. Default passed in. Value read
from storage passed out, or default if no value read.
@param Width [in/out] Width of window. Default width passed in. Value
read from storage passed out, or default if no value read.
@param Height [in/out] Height of window. Default height passed in. Value
read from storage passed out, or default if no value read.
@param State [in/out] Code describing state of window. Default state
passed in. Value read from storage passed out, or default if no value
read. This value is the ordinal value of a TWindowState value.
}
procedure SaveWdwState(const Left, Top, Width, Height, State: Integer);
override;
{Triggers OnSaveData event to request handler to store window state. If no
event handler is assigned window state is not recorded.
@param Left [in] Left side of window.
@param Top [in] Top side of window.
@param Width [in] Width of window.
@param Height [in] Height of window.
@param State [in] Code representing state of window. This is the ordinal
value of a TWindowState value.
}
published
property OnReadData: TPJWdwStateReadData
read fOnReadData write fOnReadData;
{Event triggered when window state data is to be read from persistent
storage. User must read required data in response to this event. If the
event is not handled default window state is used}
property OnSaveData: TPJWdwStateSaveData
read fOnSaveData write fOnSaveData;
{Event triggered when window state is to be written to persistent storage.
User must write provided data in response to this event. If the event is
not handled window state is not saved}
end;
{
TPJWdwStateIniRootDir:
Identifiers of the directories supported in the TPJWdwState.IniRootDir
property.
}
TPJWdwStateIniRootDir = (
rdWindowsDir, // Windows system directory: not recommended
rdExeDir, // Program directory: use for portable programs only
rdAppDataDir, // Per-user application data directory
rdProgramDataDir // Common application data directory
);
{
TPJWdwStateGetIniData:
Type of event that is triggered just before ini file is accessed. It allows
the handler to change the ini file name and section to be used.
@param AIniFileName [in/out] Value of IniFileName property passed in.
Handler can change this value. If the value passed out is a relative
path the file will be relative to the folder specified by the IniRootDir
property.
@param ASection [in/out] Default ini section name passed in. Handler can
change this value.
}
TPJWdwStateGetIniData = procedure(var AIniFilename, ASection: string)
of object;
{
TPJWdwStateGetIniDataEx:
Type of event that is triggered just before ini file is accessed. It allows
handler to change the ini root folder, file name and section to be used.
@param AIniRootDir [in/out] Value of IniRootDir property passed in.
Hander can change this value. If the value passed out in AIniFileName is
a relative path then AIniRootDir will be used to determine the folder
used to store the file.
@param AIniFileName [in/out] Value of IniFileName property passed in.
Handler can change this value. If the value passed out is a relative
path the file will be relative to the folder specified by the
AIniRootDir parameter.
@param ASection [in/out] Default ini section name passed in. Handler can
change this value.
}
TPJWdwStateGetIniDataEx = procedure(var AIniRootDir: TPJWdwStateIniRootDir;
var AIniFilename, ASection: string) of object;
{
TPJWdwState:
Implements a component that records a window's size, position and state
between program executions. An ini file is used to store the information.
}
TPJWdwState = class(TPJCustomWdwState)
private
fSection: string;
{Value of Section property}
fIniFileName: string;
{Value in IniFileName property}
fIniRootDir: TPJWdwStateIniRootDir;
{Value of IniRootDir property}
fOnGetIniData: TPJWdwStateGetIniData;
{Event handler for OnGetIniData event}
fOnGetIniDataEx: TPJWdwStateGetIniDataEx;
{Event handler for OnGetIniDataEx event}
function BuildIniFileName(AIniRootDir: TPJWdwStateIniRootDir;
AIniFileName: string): string;
{Constructs the ini file name to be used.
@param AIniRootDir [in] ID of ini file root directory use for relative
ini file names.
@param AIniFileName [in] Name of ini file. If this is a relative path
it will have a directory specified by AIniRootDir prepended.
@return Required file name. This will always be a rooted file spec.
}
function IniRootPath(const AIniRootDir: TPJWdwStateIniRootDir): string;
{Returns the root path specified by the given root directory ID. This root
directory is used for any ini file names that are relative paths.
@param AIniRootDir [in] ID of require root directory.
@return Required path. This is always a rooted path.
}
protected
procedure GetIniInfo(var AIniFileName, ASection: string);
{Triggers OnGetIniData event to get ini file and section names to be used
when restoring / saving window state.
@param AIniFileName [in/out] Required ini file name. Set to value of
IniFileName property when called. Can be changed by event handler.
@param ASection [in/out] Required section name. Set to value of Section
property when called. Can be changed by event handler.
}
procedure ReadWdwState(var Left, Top, Width, Height, State: Integer);
override;
{Reads window state from ini file.
@param Left [in/out] Left side of window. Default passed in. Value read
from ini file passed out, or default if no value read.
@param Top [in/out] Top size of window. Default passed in. Value read
from ini file passed out, or default if no value read.
@param Width [in/out] Width of window. Default width passed in. Value
read from ini file passed out, or default if no value read.
@param Height [in/out] Height of window. Default height passed in. Value
read from ini file passed out, or default if no value read.
@param State [in/out] Code describing state of window. Default state
passed in. Value read from ini file passed out, or default if no value
read. This value is the ordinal value of a TWindowState value.
}
procedure SaveWdwState(const Left, Top, Width, Height, State: Integer);
override;
{Writes window state to ini file.
@param Left [in] Left side of window.
@param Top [in] Top side of window.
@param Width [in] Width of window.
@param Height [in] Height of window.
@param State [in] Code representing state of window. This is the ordinal
value of a TWindowState value.
}
public
constructor Create(AOwner: TComponent); override;
{Class constructor. Sets default property values.
@param AOwner [in] Owning component. Must be a TForm.
@except EPJCustomWdwState raised if Owner is not a TForm.
@except EPJCustomWdwState raised if there is already a TPJCustomWdwState
component on the form.
}
function IniFilePath: string;
{Returns the fully specified file to the ini file used to store window
state information.
NOTE: This method will trigger the OnGetIniData and OnGetIniDataEx events.
}
published
// Published inherited property
property OnReadWdwState;
// New properties
property IniRootDir: TPJWdwStateIniRootDir
read fIniRootDir write fIniRootDir default rdAppDataDir;
{An identifier that specifies the root directory to be used for any
relative ini file name specified in the IniFileName property. If
IniFileName contains no path information, and IniRootDir is either
rdAppData or rdProgramData then the "DelphiDabbler\WindowStateStore\"
sub-directory of rdAppData or rdProgramData is used. The actual value used
to generate the file name can be changed in the OnGetIniDataEx event
handler.}
property IniFileName: string read fIniFileName write fIniFileName;
{The name of the ini file in which to save window information. If this
file name is a fully specified file path it is used as-is. If the file
name is relative it is stored in the root directory specified by the
IniRootDir parameter. If IniFileName is the empty string then the ini file
is has the same name as the program file, with the extension changed to
.ini. The actual value used to generate the file name can be changed in
the OnGetIniData or OnGetIniDataEx event handlers.}
property Section: string read fSection write fSection;
{The name of the section in ini file in which to save window information.
Uses "Window_<Form Name>" (eg 'Window_Form1') if set to empty string
(default). The actual section name used can be changed in the OnGetIniData
or OnGetIniDataEx event handlers.}
property OnGetIniData: TPJWdwStateGetIniData
read fOnGetIniData write fOnGetIniData;
{Event triggered just before ini file is read when restoring and saving
window state. By handling this event you can change the ini file name and
section from those specified in the IniFileName and Section properties.
NOTE 1: If a relative path is specified for the file name it will be
appended to the sub-folder specified by the IniRootDir property.
NOTE 2: The IniFileName and Section properties are not modified.
NOTE 3: This event is not triggered if OnGetIniDataEx is handled.}
property OnGetIniDataEx: TPJWdwStateGetIniDataEx
read fOnGetIniDataEx write fOnGetIniDataEx;
{Event triggered just before ini file is read when restoring and saving
window state. By handling this event you can change the default ini root
directory file name and section from those specified in the IniRootDir,
IniFileName and Section properties.
NOTE 1: If a relative path is specified for the file name it will be
appended to the sub-folder specified by the value returned in the event
handler's AIniRootDir parameter.
NOTE 2: The IniRootDir, IniFileName and Section properties are not
modified.
NOTE 3: If this event is handled then the OnGetIniData event is not
triggered.}
end;
{
TPJWdwStateGetRegData:
Type of event that is triggered just before registry is accessed. It allows
handler to change the registry root key and sub key to be used.
@param RootKey [in/out] Registry root key. Default HKEY value passed in.
May be changed in event handler.
@param SubKey [in/out] Registry sub key. Default value passed in. May be
changed in event handler.
}
TPJWdwStateGetRegData = procedure(var RootKey: HKEY;
var SubKey: string) of object;
{TPJRegRootKey:
Enumeration of values that represent the registry root keys supported by
TPJRegWdwState. Each value represents and maps to the similarly named
HKEY_* constant, as shown in the comments.
}
TPJRegRootKey = (
hkClassesRoot, // HKEY_CLASSES_ROOT
hkCurrentUser, // HKEY_CURRENT_USER
hkLocalMachine, // HKEY_LOCAL_MACHINE
hkUsers, // HKEY_USERS
hkPerformanceData, // HKEY_PERFORMANCE_DATA
hkCurrentConfig, // HKEY_CURRENT_CONFIG
hkDynData // HKEY_DYN_DATA
);
{
TPJWdwStateGetRegDataEx:
Type of event that is triggered just before registry is accessed. It allows
handler to change the registry root key and sub key to be used.
@param RootKeyEx [in/out] Registry root key. Default TPJRegRootKey value
passed in. May be changed in event handler.
@param SubKey [in/out] Registry sub key. Default value passed in. May be
changed in event handler.
}
TPJWdwStateGetRegDataEx = procedure(var RootKeyEx: TPJRegRootKey;
var SubKey: string) of object;
{
TPJWdwStateRegAccessEvent:
Type of event that is triggered after registry is opened, ready for access.
Permits handler to read / write additional data to sub key.
Added by BJM.
@param Reg [in] Reference to registry object that allows registry to be
read. Reg is set to the registry sub key where window state data is
stored and can be used to read / write additional data.
}
TPJWdwStateRegAccessEvent = procedure(const Reg: TRegistry) of object;
{
TPJRegWdwState:
Implements a component that records a window's size, position and state
between program executions. The registry is used to store the information.
NOTE: Do not use TPJRegWdwState in programs compiled with Delphi 5 and
earlier if the program is to run on 64 bit Windows: The version of TRegistry
used by these early Delphis does not fully support access to the 64 bit
registry view.
}
TPJRegWdwState = class(TPJCustomWdwState)
private // properties
fRootKeyEx: TPJRegRootKey;
{Value of RootKeyEx property}
fSubKey: string;
{Value of SubKey property}
fOnGetRegData: TPJWdwStateGetRegData;
{Event handler for OnGetRegData event}
fOnGetRegDataEx: TPJWdwStateGetRegDataEx;
{Event handler for OnGetRegDataEx event}
fOnGettingRegData: TPJWdwStateRegAccessEvent; // Added by BJM
{Event handler for OnGettingRegData event}
fOnPuttingRegData: TPJWdwStateRegAccessEvent; // Added by BJM
{Event handler for OnPuttingRegData event}
function GetRootKey: HKEY;
{Read accessor for RootKey property.
@return Required property value.
}
procedure SetRootKey(const Value: HKEY);
{Write accessor for RootKey property.
@param Value [in] New property value.
@exception ERangeError raised if value is not a recognised HKEY_* value.
}
procedure SetSubKey(const Value: string);
{Write accessor method for SubKey property.
@param Value [in] New property value. If Value='' then the property is
set to \Software\<App File Name>\Window\<Form Name>.
}
protected
procedure GetRegInfo(var ARootKey: TPJRegRootKey; var ASubKey: string);
{Triggers OnGetRegData event to get registry root key and sub key to be
used when restoring / saving window state.
@param ARootKey [in/out] Required root key value. Set to value of
RootKey property by default. May be changed in event handler.
@param ASubKey [in/ou] Required sub key. Set to value of SubKey property
when called. May be changed in event handler.
}
procedure ReadWdwState(var Left, Top, Width, Height, State: Integer);
override;
{Reads window state from registry.
@param Left [in/out] Left side of window. Default passed in. Value read
from registry passed out, or default if no value read.
@param Top [in/out] Top size of window. Default passed in. Value read
from registry passed out, or default if no value read.
@param Width [in/out] Width of window. Default width passed in. Value
read from registry passed out, or default if no value read.
@param Height [in/out] Height of window. Default height passed in. Value
read from registry passed out, or default if no value read.
@param State [in/out] Code describing state of window. Default state
passed in. Value read from registry passed out, or default if no value
read. This value is the ordinal value of a TWindowState value.
}
procedure SaveWdwState(const Left, Top, Width, Height, State: Integer);
override;
{Writes window state to registry.
@param Left [in] Left side of window.
@param Top [in] Top side of window.
@param Width [in] Width of window.
@param Height [in] Height of window.
@param State [in] Code representing state of window. This is the ordinal
value of a TWindowState value.
}
public
constructor Create(AOwner: TComponent); override;
{Class constructor. Sets default property values.
@param AOwner [in] Owning component. Must be a TForm.
@except EPJCustomWdwState raised if Owner is not a TForm.
@except EPJCustomWdwState raised if there is already a TPJCustomWdwState
component on the form.
}
published
// Published inherited property
property OnReadWdwState;
// New properties
property RootKey: HKEY read GetRootKey write SetRootKey
default HKEY_CURRENT_USER;
{Registry root key to use. Must be set to a valid HKEY value. Setting this
property also sets RootKeyEx to a corresponding value}
property RootKeyEx: TPJRegRootKey read fRootKeyEx write fRootKeyEx
stored False default hkCurrentUser;
{Registry root key to use as specified by a value from the TPJRegRootKey
enumeration. Setting this property also sets RootKey to a corresponding
value.
NOTE: This property is provided to make it easier to set root keys at
design time to avoid remembering the root key value as an integer}
property SubKey: string read fSubKey write SetSubKey;
{The sub-key below root key where window state is to be stored. If set to
empty string the value of '/Software/<Program Name>/Window/<Form Name>'
is used}
property OnGetRegData: TPJWdwStateGetRegData
read fOnGetRegData write fOnGetRegData;
{Event triggered just before registry is read when restoring and saving
window state. Allows handler to change root key and subkey to be used to
store window state. Root key is specified via its HKEY value. If this
event is handled then RootKey, RootKeyEx and SubKey properties are all
ignored}
property OnGetRegDataEx: TPJWdwStateGetRegDataEx
read fOnGetRegDataEx write fOnGetRegDataEx;
{Event triggered just before registry is read when restoring and saving
window state. Allows handler to change root key and subkey to be used to
store window state. Root key is specified via its TPJRegRootKey value. If
this event is handled then RootKey, RootKeyEx and SubKey properties are
all ignored}
property OnGettingRegData: TPJWdwStateRegAccessEvent // Added by BJM
read fOnGettingRegData write fOnGettingRegData;
{Event triggered when component is reading window state data from
registry. Handle this event to read any additional data from registry}
property OnPuttingRegData: TPJWdwStateRegAccessEvent // Added by BJM
read fOnPuttingRegData write fOnPuttingRegData;
{Event triggered when component is writing window state data to registry.
Handle this event to write any additional data to registry}
end;
procedure Register;
{Registers the components.
}
implementation
uses
// Delphi
{$IFDEF RTLNameSpaces}
System.IniFiles, Winapi.MultiMon, Vcl.StdCtrls, Winapi.ActiveX, Winapi.ShlObj
{$IFDEF TScrollStyleMoved}
, System.UITypes
{$ENDIF}
;
{$ELSE}
IniFiles, MultiMon, StdCtrls, ActiveX, ShlObj;
{$ENDIF}
{ Component registration routine }
procedure Register;
{Registers the components.
}
begin
RegisterComponents(
'DelphiDabbler',
[TPJWdwState, TPJRegWdwState, TPJUserWdwState]
);
end;
{$IFNDEF SupportsPathDelim}
// Definitions used for versions of Delphi that don't implement the following
// constant and function in SysUtils.
const
// File path delimiter
PathDelim = '/';
// Ensures that given directory or path ends with exactly one path delimiter.
function IncludeTrailingPathDelimiter(const PathOrDir: string): string;
begin
Result := PathOrDir;
// remove all trailing path delimiters if any, to get rid of any duplicates
while (Result <> '') and (Result[Length(Result)] = PathDelim) do
Result := Copy(Result, 1, Length(Result) - 1);
// add a single trailing delimiter
Result := Result + PathDelim;
end;
{$ENDIF}
{ TPJWdwStateHook }
procedure TPJWdwStateHook.CMShowingChanged(var Msg: TMessage);
{Handles CM_SHOWINGCHANGED message and dispatches it to owning window state
component.
@param Msg [in/out] The message. May be modified by message handler.
}
begin
inherited;
SendMsgToOwner(Msg);
end;
constructor TPJWdwStateHook.Create(AOwner: TComponent);
{Class constructor. Records reference to owner.
@param AOwner [in] Owning component. Must be a TPJCustomWdwState.
}
begin
Assert(Assigned(AOwner));
Assert(AOwner is TPJCustomWdwState);
inherited;
fWdwState := AOwner as TPJCustomWdwState;
end;
procedure TPJWdwStateHook.PJMSetWindowState(var Msg: TMessage);
{Handles PJM_SETWDWSTATE message and dispatches it to owning window state
component.
@param Msg [in/out] The message. May be modified by message handler.
}
begin
inherited;
SendMsgToOwner(Msg);
end;
procedure TPJWdwStateHook.SendMsgToOwner(var Msg: TMessage);
{Dispatches given message to component's owner component.
@param Msg [in/out] The message. May be changed by message handler.
}
begin
fWdwState.Dispatch(Msg);
end;
procedure TPJWdwStateHook.WMDestroy(var Msg: TMessage);
{Handles WM_DESTROY message and dispatches it to owning window state
component.
@param Msg [in/out] The message. May be modified by message handler.
}
begin
SendMsgToOwner(Msg);
inherited;
end;
{ TPJCustomWdwState }
resourcestring
// Error messages
sErrFormRequired = 'TPJCustomWdwState.Create():'#13#10
+ 'Window state components must be placed on a form. '
+ 'To create a component dynamically use the CreateStandAlone constructor.';
sErrDynamic = 'TPJCustomWdwState.CreateStandAlone():'#13#10
+ 'A non-nil parent form is required.';
sErrSingleInstance = 'TPJCustomWdwState.Create():'#13#10
+ 'Only one window state component is permitted on a form: %s is already '
+ 'present on %s.';
function TPJCustomWdwState.CanRestoreMDIChild: Boolean;
{Checks if an MDI child form can be restored.
@return True if form can be restored, False otherwise.
}
var
ParentForm: TForm; // MDI child's parent form
ParentCmp: TPJCustomWdwState; // parent form's window state control, if any
begin
ParentForm := GetMDIParentForm;
ParentCmp := GetWdwStateCmp(ParentForm);
if woFitWorkArea in fOptions then
begin
// Fitting to work area => we need information about parent form's client
// area.
if Assigned(ParentCmp) then
// Parent has a window state control that may alter client area. Therefore
// we can only restore this child form once parent has restored itself.
Result := ParentCmp.fFormRestored
else
// Parent has no window state control to alter client area. Assuming (as
// we do) that client area is known before this method is called we can
// go ahead and restore
Result := True;
end
else
// Not fitting to work area. Always OK to restore since we don't need any
// info from parent form before restoring
Result := True;
end;
procedure TPJCustomWdwState.CMShowingChanged(var Msg: TMessage);
{Message handler for owning form's CM_SHOWINGCHANGED message. The message is
sent to this component by the hook window component to indicate that the
form's showing state has changed. This causes any pending window restoration
to be executed.
@param Msg [in/out] Not used.
}
begin
inherited;
// We only act on this method the first time it's called: fFormShown indicates
// if we've been here before
if not fFormShown then
begin
// This code executed first time called only
fFormShown := True;
if not (csDesigning in ComponentState)
and (AutoSaveRestore or fRestorePending) then
// We call restore method if a call to the method is pending or we're
// auto-saving
Restore;
end;
end;
constructor TPJCustomWdwState.Create(AOwner: TComponent);
{Class constructor. Records reference to any owning form, sets default
property values and creates a hook window to trap messages from owning form.
Permits only one TPJCustomWdwState derived component to be placed on the form.
NOTE: This constructor is only suitable for components present at design time.
When constructing components dynamically use the CreateStandAlone constructor
instead.
@param AOwner [in] Owning component. Must be a TForm.
@except EPJCustomWdwState raised if Owner is not a TForm.
@except EPJCustomWdwState raised if there is already a TPJCustomWdwState
component on the form.
}
var