source: trunk/Packages/Common/UPersistentForm.pas

Last change on this file was 5, checked in by chronos, 12 years ago
  • Added: Required packages.
File size: 3.6 KB
Line 
1unit UPersistentForm;
2
3{$mode delphi}
4
5// Date: 2010-06-01
6
7interface
8
9uses
10 Classes, SysUtils, Forms, URegistry, LCLIntf, Registry;
11
12type
13
14 { TPersistentForm }
15
16 TPersistentForm = class
17 public
18 RegistryContext: TRegistryContext;
19 procedure Load(Form: TForm);
20 procedure Save(Form: TForm);
21 constructor Create;
22 end;
23
24implementation
25
26{ TPersistentForm }
27
28procedure TPersistentForm.Load(Form: TForm);
29var
30 RestoredLeft, RestoredTop, RestoredWidth, RestoredHeight: Integer;
31 NormalLeft, NormalTop, NormalWidth, NormalHeight: Integer;
32 RestoredWindowState: TWindowState;
33begin
34 with TRegistryEx.Create do
35 try
36 RootKey := RegistryContext.RootKey;
37 OpenKey(RegistryContext.Key + '\' + Form.Name, True);
38
39 RestoredWindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(Form.WindowState)));
40 if RestoredWindowState = wsMinimized then
41 RestoredWindowState := wsNormal;
42 Form.WindowState := RestoredWindowState;
43
44 if RestoredWindowState = wsNormal then begin
45 NormalLeft := ReadIntegerWithDefault('Left', (Screen.Width - Form.Width) div 2);
46 NormalTop := ReadIntegerWithDefault('Top', (Screen.Height - Form.Height) div 2);
47 NormalWidth := ReadIntegerWithDefault('Width', Form.Width);
48 NormalHeight := ReadIntegerWithDefault('Height', Form.Height);
49
50 if NormalLeft < Screen.DesktopLeft then
51 NormalLeft := Screen.DesktopLeft;
52 if NormalLeft > (Screen.DesktopWidth - 50) then
53 NormalLeft := Screen.DesktopWidth - 50;
54 if NormalTop < Screen.DesktopTop then
55 NormalTop := Screen.DesktopTop;
56 if NormalTop > (Screen.DesktopHeight - 50) then
57 NormalTop := Screen.DesktopHeight - 50;
58 if (NormalLeft <> Form.Left) or (NormalTop <> Form.Top) or
59 (NormalHeight <> Form.Height) or (NormalWidth <> Form.Width) then
60 Form.SetBounds(NormalLeft, NormalTop, NormalWidth, NormalHeight);
61 end;
62
63 if RestoredWindowState = wsMaximized then begin
64 RestoredWidth := ReadIntegerWithDefault('RestoredWidth', Form.Width);
65 RestoredHeight := ReadIntegerWithDefault('RestoredHeight', Form.Height);
66 RestoredTop := ReadIntegerWithDefault('RestoredTop', (Screen.Height - Form.Height) div 2);
67 RestoredLeft := ReadIntegerWithDefault('RestoredLeft', (Screen.Width - Form.Width) div 2);
68 if (RestoredLeft <> Form.RestoredLeft) or (RestoredTop <> Form.RestoredTop) or
69 (RestoredHeight <> Form.RestoredHeight) or (RestoredWidth <> Form.RestoredWidth) then
70 Form.SetRestoredBounds(RestoredLeft, RestoredTop, RestoredWidth, RestoredHeight);
71 end;
72
73 //if ReadBoolWithDefault('Visible', False) then Form.Show;
74 finally
75 Free;
76 end;
77end;
78
79procedure TPersistentForm.Save(Form: TForm);
80begin
81 with Form, TRegistryEx.Create do
82 try
83 RootKey := RegistryContext.RootKey;
84 OpenKey(RegistryContext.Key + '\' + Form.Name, True);
85 WriteInteger('Width', Form.Width);
86 WriteInteger('Height', Form.Height);
87 WriteInteger('Top', Form.Top);
88 WriteInteger('Left', Form.Left);
89 WriteInteger('WindowState', Integer(Form.WindowState));
90 WriteInteger('RestoredWidth', Form.RestoredWidth);
91 WriteInteger('RestoredHeight', Form.RestoredHeight);
92 WriteInteger('RestoredTop', Form.RestoredTop);
93 WriteInteger('RestoredLeft', Form.RestoredLeft);
94 WriteBool('Visible', Form.Visible);
95 finally
96 Free;
97 end;
98end;
99
100constructor TPersistentForm.Create;
101begin
102 RegistryContext.RootKey := HKEY_CURRENT_USER;
103end;
104
105end.
106
Note: See TracBrowser for help on using the repository browser.