source: trunk/Packages/Common/UPersistentForm.pas

Last change on this file was 1, checked in by chronos, 4 years ago
  • Added: "Clovece nezlob se" game with adjustable board for different player count.
File size: 11.6 KB
Line 
1unit UPersistentForm;
2
3{$mode delphi}
4
5// Date: 2020-11-26
6
7interface
8
9uses
10 Classes, SysUtils, Forms, URegistry, LCLIntf, Registry, Controls, ComCtrls,
11 ExtCtrls, LCLType;
12
13type
14
15 { TPersistentForm }
16
17 TPersistentForm = class(TComponent)
18 private
19 FEntireVisible: Boolean;
20 FMinVisiblePart: Integer;
21 FRegistryContext: TRegistryContext;
22 procedure LoadControl(Control: TControl);
23 procedure SaveControl(Control: TControl);
24 public
25 FormNormalSize: TRect;
26 FormRestoredSize: TRect;
27 FormWindowState: TWindowState;
28 FormFullScreen: Boolean;
29 Form: TForm;
30 procedure LoadFromRegistry(RegistryContext: TRegistryContext);
31 procedure SaveToRegistry(RegistryContext: TRegistryContext);
32 function CheckEntireVisible(Rect: TRect): TRect;
33 function CheckPartVisible(Rect: TRect; Part: Integer): TRect;
34 procedure Load(Form: TForm; DefaultMaximized: Boolean = False;
35 DefaultFullScreen: Boolean = False);
36 procedure Save(Form: TForm);
37 constructor Create(AOwner: TComponent); override;
38 procedure SetFullScreen(State: Boolean);
39 property RegistryContext: TRegistryContext read FRegistryContext
40 write FRegistryContext;
41 published
42 property MinVisiblePart: Integer read FMinVisiblePart write FMinVisiblePart;
43 property EntireVisible: Boolean read FEntireVisible write FEntireVisible;
44 end;
45
46procedure Register;
47
48
49implementation
50
51procedure Register;
52begin
53 RegisterComponents('Common', [TPersistentForm]);
54end;
55
56{ TPersistentForm }
57
58procedure TPersistentForm.LoadControl(Control: TControl);
59var
60 I: Integer;
61 WinControl: TWinControl;
62begin
63 if Control is TListView then begin
64 with Form, TRegistryEx.Create do
65 try
66 RootKey := RegistryContext.RootKey;
67 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name + '\' + Control.Name, True);
68 for I := 0 to TListView(Control).Columns.Count - 1 do begin
69 if ValueExists('ColWidth' + IntToStr(I)) then
70 TListView(Control).Columns[I].Width := ReadInteger('ColWidth' + IntToStr(I));
71 end;
72 finally
73 Free;
74 end;
75 end;
76
77 if (Control is TPanel) then begin
78 with Form, TRegistryEx.Create do
79 try
80 RootKey := RegistryContext.RootKey;
81 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name + '\' + Control.Name, True);
82 if (TPanel(Control).Align = alRight) or (TPanel(Control).Align = alLeft) then begin
83 if ValueExists('Width') then
84 TPanel(Control).Width := ReadInteger('Width');
85 end;
86 if (TPanel(Control).Align = alTop) or (TPanel(Control).Align = alBottom) then begin
87 if ValueExists('Height') then
88 TPanel(Control).Height := ReadInteger('Height');
89 end;
90 finally
91 Free;
92 end;
93 end;
94
95 if Control is TWinControl then begin
96 WinControl := TWinControl(Control);
97 if WinControl.ControlCount > 0 then begin
98 for I := 0 to WinControl.ControlCount - 1 do begin
99 if WinControl.Controls[I] is TControl then begin
100 LoadControl(WinControl.Controls[I]);
101 end;
102 end;
103 end;
104 end;
105end;
106
107procedure TPersistentForm.SaveControl(Control: TControl);
108var
109 I: Integer;
110 WinControl: TWinControl;
111begin
112 if Control is TListView then begin
113 with Form, TRegistryEx.Create do
114 try
115 RootKey := RegistryContext.RootKey;
116 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name + '\' + Control.Name, True);
117 for I := 0 to TListView(Control).Columns.Count - 1 do begin
118 WriteInteger('ColWidth' + IntToStr(I), TListView(Control).Columns[I].Width);
119 end;
120 finally
121 Free;
122 end;
123 end;
124
125 if (Control is TPanel) then begin
126 with Form, TRegistryEx.Create do
127 try
128 RootKey := RegistryContext.RootKey;
129 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name + '\' + Control.Name, True);
130 if (TPanel(Control).Align = alRight) or (TPanel(Control).Align = alLeft) then begin
131 WriteInteger('Width', TPanel(Control).Width);
132 end;
133 if (TPanel(Control).Align = alTop) or (TPanel(Control).Align = alBottom) then begin
134 WriteInteger('Height', TPanel(Control).Height);
135 end;
136 finally
137 Free;
138 end;
139 end;
140
141 if Control is TWinControl then begin
142 WinControl := TWinControl(Control);
143 if WinControl.ControlCount > 0 then begin
144 for I := 0 to WinControl.ControlCount - 1 do begin
145 if WinControl.Controls[I] is TControl then begin
146 SaveControl(WinControl.Controls[I]);
147 end;
148 end;
149 end;
150 end;
151end;
152
153procedure TPersistentForm.LoadFromRegistry(RegistryContext: TRegistryContext);
154begin
155 with TRegistryEx.Create do
156 try
157 RootKey := RegistryContext.RootKey;
158 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True);
159 // Normal size
160 FormNormalSize.Left := ReadIntegerWithDefault('NormalLeft', FormNormalSize.Left);
161 FormNormalSize.Top := ReadIntegerWithDefault('NormalTop', FormNormalSize.Top);
162 FormNormalSize.Right := ReadIntegerWithDefault('NormalWidth', FormNormalSize.Right - FormNormalSize.Left)
163 + FormNormalSize.Left;
164 FormNormalSize.Bottom := ReadIntegerWithDefault('NormalHeight', FormNormalSize.Bottom - FormNormalSize.Top)
165 + FormNormalSize.Top;
166 // Restored size
167 FormRestoredSize.Left := ReadIntegerWithDefault('RestoredLeft', FormRestoredSize.Left);
168 FormRestoredSize.Top := ReadIntegerWithDefault('RestoredTop', FormRestoredSize.Top);
169 FormRestoredSize.Right := ReadIntegerWithDefault('RestoredWidth', FormRestoredSize.Right - FormRestoredSize.Left)
170 + FormRestoredSize.Left;
171 FormRestoredSize.Bottom := ReadIntegerWithDefault('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top)
172 + FormRestoredSize.Top;
173 // Other state
174 FormWindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(FormWindowState)));
175 FormFullScreen := ReadBoolWithDefault('FullScreen', FormFullScreen);
176 finally
177 Free;
178 end;
179end;
180
181procedure TPersistentForm.SaveToRegistry(RegistryContext: TRegistryContext);
182begin
183 with Form, TRegistryEx.Create do
184 try
185 RootKey := RegistryContext.RootKey;
186 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True);
187 // Normal state
188 WriteInteger('NormalWidth', FormNormalSize.Right - FormNormalSize.Left);
189 WriteInteger('NormalHeight', FormNormalSize.Bottom - FormNormalSize.Top);
190 WriteInteger('NormalTop', FormNormalSize.Top);
191 WriteInteger('NormalLeft', FormNormalSize.Left);
192 // Restored state
193 WriteInteger('RestoredWidth', FormRestoredSize.Right - FormRestoredSize.Left);
194 WriteInteger('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top);
195 WriteInteger('RestoredTop', FormRestoredSize.Top);
196 WriteInteger('RestoredLeft', FormRestoredSize.Left);
197 // Other state
198 WriteInteger('WindowState', Integer(FormWindowState));
199 WriteBool('FullScreen', FormFullScreen);
200 finally
201 Free;
202 end;
203end;
204
205function TPersistentForm.CheckEntireVisible(Rect: TRect): TRect;
206var
207 Width: Integer;
208 Height: Integer;
209begin
210 Result := Rect;
211 Width := Rect.Right - Rect.Left;
212 Height := Rect.Bottom - Rect.Top;
213 if Result.Left < (Screen.DesktopLeft) then begin
214 Result.Left := Screen.DesktopLeft;
215 Result.Right := Screen.DesktopLeft + Width;
216 end;
217 if Result.Right > (Screen.DesktopLeft + Screen.DesktopWidth) then begin
218 Result.Left := Screen.DesktopLeft + Screen.DesktopWidth - Width;
219 Result.Right := Screen.DesktopLeft + Screen.DesktopWidth;
220 end;
221 if Result.Top < Screen.DesktopTop then begin
222 Result.Top := Screen.DesktopTop;
223 Result.Bottom := Screen.DesktopTop + Height;
224 end;
225 if Result.Bottom > (Screen.DesktopTop + Screen.DesktopHeight) then begin
226 Result.Top := Screen.DesktopTop + Screen.DesktopHeight - Height;
227 Result.Bottom := Screen.DesktopTop + Screen.DesktopHeight;
228 end;
229end;
230
231function TPersistentForm.CheckPartVisible(Rect: TRect; Part: Integer): TRect;
232var
233 Width: Integer;
234 Height: Integer;
235begin
236 Result := Rect;
237 Width := Rect.Right - Rect.Left;
238 Height := Rect.Bottom - Rect.Top;
239 if Result.Right < (Screen.DesktopLeft + Part) then begin
240 Result.Left := Screen.DesktopLeft + Part - Width;
241 Result.Right := Screen.DesktopLeft + Part;
242 end;
243 if Result.Left > (Screen.DesktopLeft + Screen.DesktopWidth - Part) then begin
244 Result.Left := Screen.DesktopLeft + Screen.DesktopWidth - Part;
245 Result.Right := Screen.DesktopLeft + Screen.DesktopWidth - Part + Width;
246 end;
247 if Result.Bottom < (Screen.DesktopTop + Part) then begin
248 Result.Top := Screen.DesktopTop + Part - Height;
249 Result.Bottom := Screen.DesktopTop + Part;
250 end;
251 if Result.Top > (Screen.DesktopTop + Screen.DesktopHeight - Part) then begin
252 Result.Top := Screen.DesktopTop + Screen.DesktopHeight - Part;
253 Result.Bottom := Screen.DesktopTop + Screen.DesktopHeight - Part + Height;
254 end;
255end;
256
257procedure TPersistentForm.Load(Form: TForm; DefaultMaximized: Boolean = False;
258 DefaultFullScreen: Boolean = False);
259begin
260 Self.Form := Form;
261 // Set default
262 FormNormalSize := Bounds((Screen.Width - Form.Width) div 2,
263 (Screen.Height - Form.Height) div 2, Form.Width, Form.Height);
264 FormRestoredSize := Bounds((Screen.Width - Form.Width) div 2,
265 (Screen.Height - Form.Height) div 2, Form.Width, Form.Height);
266 FormWindowState := Form.WindowState;
267 FormFullScreen := DefaultFullScreen;
268
269 LoadFromRegistry(RegistryContext);
270
271 if not EqualRect(FormNormalSize, FormRestoredSize) or
272 DefaultMaximized then begin
273 // Restore to maximized state
274 Form.WindowState := wsNormal;
275 if not EqualRect(FormRestoredSize, Form.BoundsRect) then
276 Form.BoundsRect := FormRestoredSize;
277 Form.WindowState := wsMaximized;
278 end else begin
279 // Restore to normal state
280 Form.WindowState := wsNormal;
281 if FEntireVisible then FormNormalSize := CheckEntireVisible(FormNormalSize)
282 else if FMinVisiblePart > 0 then
283 FormNormalSize := CheckPartVisible(FormNormalSize, FMinVisiblePart);
284 if not EqualRect(FormNormalSize, Form.BoundsRect) then
285 Form.BoundsRect := FormNormalSize;
286 end;
287 if FormFullScreen then SetFullScreen(True);
288 LoadControl(Form);
289end;
290
291procedure TPersistentForm.Save(Form: TForm);
292begin
293 Self.Form := Form;
294 FormNormalSize := Bounds(Form.Left, Form.Top, Form.Width, Form.Height);
295 if not FormFullScreen then
296 FormRestoredSize := Bounds(Form.RestoredLeft, Form.RestoredTop, Form.RestoredWidth,
297 Form.RestoredHeight);
298 FormWindowState := Form.WindowState;
299 SaveToRegistry(RegistryContext);
300 SaveControl(Form);
301end;
302
303constructor TPersistentForm.Create(AOwner: TComponent);
304begin
305 inherited;
306 if AOwner is TForm then Form := TForm(AOwner)
307 else Form := nil;
308 FMinVisiblePart := 50;
309 FRegistryContext.RootKey := HKEY_CURRENT_USER;
310end;
311
312procedure TPersistentForm.SetFullScreen(State: Boolean);
313begin
314 if State then begin
315 FormFullScreen := True;
316 FormNormalSize := Form.BoundsRect;
317 FormRestoredSize := Bounds(Form.RestoredLeft, Form.RestoredTop, Form.RestoredWidth,
318 Form.RestoredHeight);
319 FormWindowState := Form.WindowState;
320 ShowWindow(Form.Handle, SW_SHOWFULLSCREEN);
321 {$IFDEF WINDOWS}
322 Form.BorderStyle := bsNone;
323 {$ENDIF}
324 end else begin
325 FormFullScreen := False;
326 {$IFDEF WINDOWS}
327 Form.BorderStyle := bsSizeable;
328 {$ENDIF}
329 ShowWindow(Form.Handle, SW_SHOWNORMAL);
330 if FormWindowState = wsNormal then begin
331 Form.BoundsRect := FormNormalSize;
332 end else
333 if FormWindowState = wsMaximized then begin
334 Form.BoundsRect := FormRestoredSize;
335 Form.WindowState := wsMaximized;
336 end;
337 end;
338end;
339
340end.
341
Note: See TracBrowser for help on using the repository browser.