source: trunk/Packages/Common/UPersistentForm.pas

Last change on this file was 2, checked in by chronos, 9 years ago
  • Added: TPakFile class for extraction PAK files from original game.
File size: 7.2 KB
Line 
1unit UPersistentForm;
2
3{$mode delphi}
4
5// Date: 2015-04-18
6
7interface
8
9uses
10 Classes, SysUtils, Forms, URegistry, LCLIntf, Registry;
11
12type
13
14 { TPersistentForm }
15
16 TPersistentForm = class(TComponent)
17 private
18 FEntireVisible: Boolean;
19 FMinVisiblePart: Integer;
20 FRegistryContext: TRegistryContext;
21 public
22 FormNormalSize: TRect;
23 FormRestoredSize: TRect;
24 FormWindowState: TWindowState;
25 Form: TForm;
26 procedure LoadFromRegistry(RegistryContext: TRegistryContext);
27 procedure SaveToRegistry(RegistryContext: TRegistryContext);
28 function CheckEntireVisible(Rect: TRect): TRect;
29 function CheckPartVisible(Rect: TRect; Part: Integer): TRect;
30 procedure Load(Form: TForm; DefaultMaximized: Boolean = False);
31 procedure Save(Form: TForm);
32 constructor Create(AOwner: TComponent); override;
33 property RegistryContext: TRegistryContext read FRegistryContext
34 write FRegistryContext;
35 published
36 property MinVisiblePart: Integer read FMinVisiblePart write FMinVisiblePart;
37 property EntireVisible: Boolean read FEntireVisible write FEntireVisible;
38 end;
39
40procedure Register;
41
42implementation
43
44
45procedure Register;
46begin
47 RegisterComponents('Common', [TPersistentForm]);
48end;
49
50{ TPersistentForm }
51
52procedure TPersistentForm.LoadFromRegistry(RegistryContext: TRegistryContext);
53begin
54 with TRegistryEx.Create do
55 try
56 RootKey := RegistryContext.RootKey;
57 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True);
58 // Normal size
59 FormNormalSize.Left := ReadIntegerWithDefault('NormalLeft', FormNormalSize.Left);
60 FormNormalSize.Top := ReadIntegerWithDefault('NormalTop', FormNormalSize.Top);
61 FormNormalSize.Right := ReadIntegerWithDefault('NormalWidth', FormNormalSize.Right - FormNormalSize.Left)
62 + FormNormalSize.Left;
63 FormNormalSize.Bottom := ReadIntegerWithDefault('NormalHeight', FormNormalSize.Bottom - FormNormalSize.Top)
64 + FormNormalSize.Top;
65 // Restored size
66 FormRestoredSize.Left := ReadIntegerWithDefault('RestoredLeft', FormRestoredSize.Left);
67 FormRestoredSize.Top := ReadIntegerWithDefault('RestoredTop', FormRestoredSize.Top);
68 FormRestoredSize.Right := ReadIntegerWithDefault('RestoredWidth', FormRestoredSize.Right - FormRestoredSize.Left)
69 + FormRestoredSize.Left;
70 FormRestoredSize.Bottom := ReadIntegerWithDefault('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top)
71 + FormRestoredSize.Top;
72 // Other state
73 FormWindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(wsNormal)));
74 finally
75 Free;
76 end;
77end;
78
79procedure TPersistentForm.SaveToRegistry(RegistryContext: TRegistryContext);
80begin
81 with Form, TRegistryEx.Create do
82 try
83 RootKey := RegistryContext.RootKey;
84 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True);
85 // Normal state
86 WriteInteger('NormalWidth', FormNormalSize.Right - FormNormalSize.Left);
87 WriteInteger('NormalHeight', FormNormalSize.Bottom - FormNormalSize.Top);
88 WriteInteger('NormalTop', FormNormalSize.Top);
89 WriteInteger('NormalLeft', FormNormalSize.Left);
90 // Restored state
91 WriteInteger('RestoredWidth', FormRestoredSize.Right - FormRestoredSize.Left);
92 WriteInteger('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top);
93 WriteInteger('RestoredTop', FormRestoredSize.Top);
94 WriteInteger('RestoredLeft', FormRestoredSize.Left);
95 // Other state
96 WriteInteger('WindowState', Integer(FormWindowState));
97 finally
98 Free;
99 end;
100end;
101
102function TPersistentForm.CheckEntireVisible(Rect: TRect): TRect;
103var
104 Width: Integer;
105 Height: Integer;
106begin
107 Result := Rect;
108 Width := Rect.Right - Rect.Left;
109 Height := Rect.Bottom - Rect.Top;
110 if Result.Left < (Screen.DesktopLeft) then begin
111 Result.Left := Screen.DesktopLeft;
112 Result.Right := Screen.DesktopLeft + Width;
113 end;
114 if Result.Right > (Screen.DesktopLeft + Screen.DesktopWidth) then begin
115 Result.Left := Screen.DesktopLeft + Screen.DesktopWidth - Width;
116 Result.Right := Screen.DesktopLeft + Screen.DesktopWidth;
117 end;
118 if Result.Top < Screen.DesktopTop then begin
119 Result.Top := Screen.DesktopTop;
120 Result.Bottom := Screen.DesktopTop + Height;
121 end;
122 if Result.Bottom > (Screen.DesktopTop + Screen.DesktopHeight) then begin
123 Result.Top := Screen.DesktopTop + Screen.DesktopHeight - Height;
124 Result.Bottom := Screen.DesktopTop + Screen.DesktopHeight;
125 end;
126end;
127
128function TPersistentForm.CheckPartVisible(Rect: TRect; Part: Integer): TRect;
129var
130 Width: Integer;
131 Height: Integer;
132begin
133 Result := Rect;
134 Width := Rect.Right - Rect.Left;
135 Height := Rect.Bottom - Rect.Top;
136 if Result.Right < (Screen.DesktopLeft + Part) then begin
137 Result.Left := Screen.DesktopLeft + Part - Width;
138 Result.Right := Screen.DesktopLeft + Part;
139 end;
140 if Result.Left > (Screen.DesktopLeft + Screen.DesktopWidth - Part) then begin
141 Result.Left := Screen.DesktopLeft + Screen.DesktopWidth - Part;
142 Result.Right := Screen.DesktopLeft + Screen.DesktopWidth - Part + Width;
143 end;
144 if Result.Bottom < (Screen.DesktopTop + Part) then begin
145 Result.Top := Screen.DesktopTop + Part - Height;
146 Result.Bottom := Screen.DesktopTop + Part;
147 end;
148 if Result.Top > (Screen.DesktopTop + Screen.DesktopHeight - Part) then begin
149 Result.Top := Screen.DesktopTop + Screen.DesktopHeight - Part;
150 Result.Bottom := Screen.DesktopTop + Screen.DesktopHeight - Part + Height;
151 end;
152end;
153
154procedure TPersistentForm.Load(Form: TForm; DefaultMaximized: Boolean = False);
155var
156 LoadDefaults: Boolean;
157begin
158 Self.Form := Form;
159 // Set default
160 FormNormalSize := Bounds((Screen.Width - Form.Width) div 2,
161 (Screen.Height - Form.Height) div 2, Form.Width, Form.Height);
162 FormRestoredSize := Bounds((Screen.Width - Form.Width) div 2,
163 (Screen.Height - Form.Height) div 2, Form.Width, Form.Height);
164
165 LoadFromRegistry(RegistryContext);
166
167 if not EqualRect(FormNormalSize, FormRestoredSize) or
168 (LoadDefaults and DefaultMaximized) then begin
169 // Restore to maximized state
170 Form.WindowState := wsNormal;
171 if not EqualRect(FormRestoredSize, Form.BoundsRect) then
172 Form.BoundsRect := FormRestoredSize;
173 Form.WindowState := wsMaximized;
174 end else begin
175 // Restore to normal state
176 Form.WindowState := wsNormal;
177 if FEntireVisible then FormNormalSize := CheckEntireVisible(FormNormalSize)
178 else if FMinVisiblePart > 0 then
179 FormNormalSize := CheckPartVisible(FormNormalSize, FMinVisiblePart);
180 if not EqualRect(FormNormalSize, Form.BoundsRect) then
181 Form.BoundsRect := FormNormalSize;
182 end;
183end;
184
185procedure TPersistentForm.Save(Form: TForm);
186begin
187 Self.Form := Form;
188 FormNormalSize := Bounds(Form.Left, Form.Top, Form.Width, Form.Height);
189 FormRestoredSize := Bounds(Form.RestoredLeft, Form.RestoredTop, Form.RestoredWidth,
190 Form.RestoredHeight);
191 FormWindowState := Form.WindowState;
192 SaveToRegistry(RegistryContext);
193end;
194
195constructor TPersistentForm.Create(AOwner: TComponent);
196begin
197 inherited;
198 if AOwner is TForm then Form := TForm(AOwner)
199 else Form := nil;
200 FMinVisiblePart := 50;
201 FRegistryContext.RootKey := HKEY_CURRENT_USER;
202end;
203
204end.
205
Note: See TracBrowser for help on using the repository browser.