| 1 | unit UPersistentForm;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}
|
|---|
| 4 |
|
|---|
| 5 | // Date: 2010-06-01
|
|---|
| 6 |
|
|---|
| 7 | interface
|
|---|
| 8 |
|
|---|
| 9 | uses
|
|---|
| 10 | Classes, SysUtils, Forms, URegistry, LCLIntf, MyRegistry;
|
|---|
| 11 |
|
|---|
| 12 | type
|
|---|
| 13 |
|
|---|
| 14 | { TPersistentForm }
|
|---|
| 15 |
|
|---|
| 16 | TPersistentForm = class(TComponent)
|
|---|
| 17 | private
|
|---|
| 18 | FEntireVisible: Boolean;
|
|---|
| 19 | FMinVisiblePart: Integer;
|
|---|
| 20 | FRegistryContext: TRegistryContext;
|
|---|
| 21 | public
|
|---|
| 22 | function CheckEntireVisible(Rect: TRect): TRect;
|
|---|
| 23 | function CheckPartVisible(Rect: TRect; Part: Integer): TRect;
|
|---|
| 24 | procedure Load(Form: TForm; DefaultMaximized: Boolean = False);
|
|---|
| 25 | procedure Save(Form: TForm);
|
|---|
| 26 | constructor Create(AOwner: TComponent); override;
|
|---|
| 27 | property RegistryContext: TRegistryContext read FRegistryContext
|
|---|
| 28 | write FRegistryContext;
|
|---|
| 29 | published
|
|---|
| 30 | property MinVisiblePart: Integer read FMinVisiblePart write FMinVisiblePart;
|
|---|
| 31 | property EntireVisible: Boolean read FEntireVisible write FEntireVisible;
|
|---|
| 32 | end;
|
|---|
| 33 |
|
|---|
| 34 | procedure Register;
|
|---|
| 35 |
|
|---|
| 36 | implementation
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | procedure Register;
|
|---|
| 40 | begin
|
|---|
| 41 | RegisterComponents('Common', [TPersistentForm]);
|
|---|
| 42 | end;
|
|---|
| 43 |
|
|---|
| 44 | { TPersistentForm }
|
|---|
| 45 |
|
|---|
| 46 | function TPersistentForm.CheckEntireVisible(Rect: TRect): TRect;
|
|---|
| 47 | var
|
|---|
| 48 | Width: Integer;
|
|---|
| 49 | Height: Integer;
|
|---|
| 50 | begin
|
|---|
| 51 | Result := Rect;
|
|---|
| 52 | Width := Rect.Right - Rect.Left;
|
|---|
| 53 | Height := Rect.Bottom - Rect.Top;
|
|---|
| 54 | if Result.Left < (Screen.DesktopLeft) then begin
|
|---|
| 55 | Result.Left := Screen.DesktopLeft;
|
|---|
| 56 | Result.Right := Screen.DesktopLeft + Width;
|
|---|
| 57 | end;
|
|---|
| 58 | if Result.Right > (Screen.DesktopLeft + Screen.DesktopWidth) then begin
|
|---|
| 59 | Result.Left := Screen.DesktopLeft + Screen.DesktopWidth - Width;
|
|---|
| 60 | Result.Right := Screen.DesktopLeft + Screen.DesktopWidth;
|
|---|
| 61 | end;
|
|---|
| 62 | if Result.Top < Screen.DesktopTop then begin
|
|---|
| 63 | Result.Top := Screen.DesktopTop;
|
|---|
| 64 | Result.Bottom := Screen.DesktopTop + Height;
|
|---|
| 65 | end;
|
|---|
| 66 | if Result.Bottom > (Screen.DesktopTop + Screen.DesktopHeight) then begin
|
|---|
| 67 | Result.Top := Screen.DesktopTop + Screen.DesktopHeight - Height;
|
|---|
| 68 | Result.Bottom := Screen.DesktopTop + Screen.DesktopHeight;
|
|---|
| 69 | end;
|
|---|
| 70 | end;
|
|---|
| 71 |
|
|---|
| 72 | function TPersistentForm.CheckPartVisible(Rect: TRect; Part: Integer): TRect;
|
|---|
| 73 | var
|
|---|
| 74 | Width: Integer;
|
|---|
| 75 | Height: Integer;
|
|---|
| 76 | begin
|
|---|
| 77 | Result := Rect;
|
|---|
| 78 | Width := Rect.Right - Rect.Left;
|
|---|
| 79 | Height := Rect.Bottom - Rect.Top;
|
|---|
| 80 | if Result.Right < (Screen.DesktopLeft + Part) then begin
|
|---|
| 81 | Result.Left := Screen.DesktopLeft + Part - Width;
|
|---|
| 82 | Result.Right := Screen.DesktopLeft + Part;
|
|---|
| 83 | end;
|
|---|
| 84 | if Result.Left > (Screen.DesktopLeft + Screen.DesktopWidth - Part) then begin
|
|---|
| 85 | Result.Left := Screen.DesktopLeft + Screen.DesktopWidth - Part;
|
|---|
| 86 | Result.Right := Screen.DesktopLeft + Screen.DesktopWidth - Part + Width;
|
|---|
| 87 | end;
|
|---|
| 88 | if Result.Bottom < (Screen.DesktopTop + Part) then begin
|
|---|
| 89 | Result.Top := Screen.DesktopTop + Part - Height;
|
|---|
| 90 | Result.Bottom := Screen.DesktopTop + Part;
|
|---|
| 91 | end;
|
|---|
| 92 | if Result.Top > (Screen.DesktopTop + Screen.DesktopHeight - Part) then begin
|
|---|
| 93 | Result.Top := Screen.DesktopTop + Screen.DesktopHeight - Part;
|
|---|
| 94 | Result.Bottom := Screen.DesktopTop + Screen.DesktopHeight - Part + Height;
|
|---|
| 95 | end;
|
|---|
| 96 | end;
|
|---|
| 97 |
|
|---|
| 98 | procedure TPersistentForm.Load(Form: TForm; DefaultMaximized: Boolean = False);
|
|---|
| 99 | var
|
|---|
| 100 | Normal: TRect;
|
|---|
| 101 | Restored: TRect;
|
|---|
| 102 | LoadDefaults: Boolean;
|
|---|
| 103 | begin
|
|---|
| 104 | with TRegistryEx.Create do
|
|---|
| 105 | try
|
|---|
| 106 | RootKey := RegistryContext.RootKey;
|
|---|
| 107 | OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True);
|
|---|
| 108 |
|
|---|
| 109 | //RestoredWindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(Form.WindowState)));
|
|---|
| 110 | //if RestoredWindowState = wsMinimized then
|
|---|
| 111 | // RestoredWindowState := wsNormal;
|
|---|
| 112 | //Form.WindowState := RestoredWindowState;
|
|---|
| 113 | LoadDefaults := not ValueExists('NormalLeft');
|
|---|
| 114 | Normal := Bounds(ReadIntegerWithDefault('NormalLeft', (Screen.Width - Form.Width) div 2),
|
|---|
| 115 | ReadIntegerWithDefault('NormalTop', (Screen.Height - Form.Height) div 2),
|
|---|
| 116 | ReadIntegerWithDefault('NormalWidth', Form.Width),
|
|---|
| 117 | ReadIntegerWithDefault('NormalHeight', Form.Height));
|
|---|
| 118 | Restored := Bounds(ReadIntegerWithDefault('RestoredLeft', (Screen.Width - Form.Width) div 2),
|
|---|
| 119 | ReadIntegerWithDefault('RestoredTop', (Screen.Height - Form.Height) div 2),
|
|---|
| 120 | ReadIntegerWithDefault('RestoredWidth', Form.Width),
|
|---|
| 121 | ReadIntegerWithDefault('RestoredHeight', Form.Height));
|
|---|
| 122 |
|
|---|
| 123 | if not EqualRect(Normal, Restored) or
|
|---|
| 124 | (LoadDefaults and DefaultMaximized) then begin
|
|---|
| 125 | // Restore to maximized state
|
|---|
| 126 | Form.WindowState := wsNormal;
|
|---|
| 127 | if not EqualRect(Restored, Form.BoundsRect) then
|
|---|
| 128 | Form.BoundsRect := Restored;
|
|---|
| 129 | Form.WindowState := wsMaximized;
|
|---|
| 130 | end else begin
|
|---|
| 131 | // Restore to normal state
|
|---|
| 132 | Form.WindowState := wsNormal;
|
|---|
| 133 | if FEntireVisible then Normal := CheckEntireVisible(Normal)
|
|---|
| 134 | else if FMinVisiblePart > 0 then
|
|---|
| 135 | Normal := CheckPartVisible(Normal, FMinVisiblePart);
|
|---|
| 136 | if not EqualRect(Normal, Form.BoundsRect) then
|
|---|
| 137 | Form.BoundsRect := Normal;
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 | //if ReadBoolWithDefault('Visible', False) then Form.Show;
|
|---|
| 141 | finally
|
|---|
| 142 | Free;
|
|---|
| 143 | end;
|
|---|
| 144 | end;
|
|---|
| 145 |
|
|---|
| 146 | procedure TPersistentForm.Save(Form: TForm);
|
|---|
| 147 | begin
|
|---|
| 148 | with Form, TRegistryEx.Create do
|
|---|
| 149 | try
|
|---|
| 150 | RootKey := RegistryContext.RootKey;
|
|---|
| 151 | OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True);
|
|---|
| 152 | WriteInteger('NormalWidth', Form.Width);
|
|---|
| 153 | WriteInteger('NormalHeight', Form.Height);
|
|---|
| 154 | WriteInteger('NormalTop', Form.Top);
|
|---|
| 155 | WriteInteger('NormalLeft', Form.Left);
|
|---|
| 156 | WriteInteger('RestoredWidth', Form.RestoredWidth);
|
|---|
| 157 | WriteInteger('RestoredHeight', Form.RestoredHeight);
|
|---|
| 158 | WriteInteger('RestoredTop', Form.RestoredTop);
|
|---|
| 159 | WriteInteger('RestoredLeft', Form.RestoredLeft);
|
|---|
| 160 | //WriteInteger('WindowState', Integer(Form.WindowState));
|
|---|
| 161 | //WriteBool('Visible', Form.Visible);
|
|---|
| 162 | finally
|
|---|
| 163 | Free;
|
|---|
| 164 | end;
|
|---|
| 165 | end;
|
|---|
| 166 |
|
|---|
| 167 | constructor TPersistentForm.Create(AOwner: TComponent);
|
|---|
| 168 | begin
|
|---|
| 169 | inherited;
|
|---|
| 170 | FMinVisiblePart := 50;
|
|---|
| 171 | FRegistryContext.RootKey := HKEY_CURRENT_USER;
|
|---|
| 172 | end;
|
|---|
| 173 |
|
|---|
| 174 | end.
|
|---|
| 175 |
|
|---|