Changeset 15 for trunk/Packages/Common/UPersistentForm.pas
- Timestamp:
- Mar 22, 2018, 8:31:19 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/UPersistentForm.pas
r10 r15 3 3 {$mode delphi} 4 4 5 // Date: 201 0-06-015 // Date: 2015-04-18 6 6 7 7 interface 8 8 9 9 uses 10 Classes, SysUtils, Forms, URegistry, LCLIntf, Registry ;10 Classes, SysUtils, Forms, URegistry, LCLIntf, Registry, Controls, ComCtrls; 11 11 12 12 type … … 19 19 FMinVisiblePart: Integer; 20 20 FRegistryContext: TRegistryContext; 21 procedure LoadControl(Control: TControl); 22 procedure SaveControl(Control: TControl); 21 23 public 24 FormNormalSize: TRect; 25 FormRestoredSize: TRect; 26 FormWindowState: TWindowState; 27 Form: TForm; 28 procedure LoadFromRegistry(RegistryContext: TRegistryContext); 29 procedure SaveToRegistry(RegistryContext: TRegistryContext); 22 30 function CheckEntireVisible(Rect: TRect): TRect; 23 31 function CheckPartVisible(Rect: TRect; Part: Integer): TRect; … … 44 52 { TPersistentForm } 45 53 54 procedure TPersistentForm.LoadControl(Control: TControl); 55 var 56 I: Integer; 57 WinControl: TWinControl; 58 Count: Integer; 59 begin 60 if Control is TListView then begin 61 with Form, TRegistryEx.Create do 62 try 63 RootKey := RegistryContext.RootKey; 64 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name + '\' + Control.Name, True); 65 for I := 0 to TListView(Control).Columns.Count - 1 do begin 66 if ValueExists('ColWidth' + IntToStr(I)) then 67 TListView(Control).Columns[I].Width := ReadInteger('ColWidth' + IntToStr(I)); 68 end; 69 finally 70 Free; 71 end; 72 end; 73 74 if Control is TWinControl then begin 75 WinControl := TWinControl(Control); 76 if WinControl.ControlCount > 0 then begin 77 for I := 0 to WinControl.ControlCount - 1 do begin 78 if WinControl.Controls[I] is TControl then begin 79 LoadControl(WinControl.Controls[I]); 80 end; 81 end; 82 end; 83 end; 84 end; 85 86 procedure TPersistentForm.SaveControl(Control: TControl); 87 var 88 I: Integer; 89 WinControl: TWinControl; 90 begin 91 if Control is TListView then begin 92 with Form, TRegistryEx.Create do 93 try 94 RootKey := RegistryContext.RootKey; 95 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name + '\' + Control.Name, True); 96 for I := 0 to TListView(Control).Columns.Count - 1 do begin 97 WriteInteger('ColWidth' + IntToStr(I), TListView(Control).Columns[I].Width); 98 end; 99 finally 100 Free; 101 end; 102 end; 103 104 if Control is TWinControl then begin 105 WinControl := TWinControl(Control); 106 if WinControl.ControlCount > 0 then begin 107 for I := 0 to WinControl.ControlCount - 1 do begin 108 if WinControl.Controls[I] is TControl then begin 109 SaveControl(WinControl.Controls[I]); 110 end; 111 end; 112 end; 113 end; 114 end; 115 116 procedure TPersistentForm.LoadFromRegistry(RegistryContext: TRegistryContext); 117 begin 118 with TRegistryEx.Create do 119 try 120 RootKey := RegistryContext.RootKey; 121 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True); 122 // Normal size 123 FormNormalSize.Left := ReadIntegerWithDefault('NormalLeft', FormNormalSize.Left); 124 FormNormalSize.Top := ReadIntegerWithDefault('NormalTop', FormNormalSize.Top); 125 FormNormalSize.Right := ReadIntegerWithDefault('NormalWidth', FormNormalSize.Right - FormNormalSize.Left) 126 + FormNormalSize.Left; 127 FormNormalSize.Bottom := ReadIntegerWithDefault('NormalHeight', FormNormalSize.Bottom - FormNormalSize.Top) 128 + FormNormalSize.Top; 129 // Restored size 130 FormRestoredSize.Left := ReadIntegerWithDefault('RestoredLeft', FormRestoredSize.Left); 131 FormRestoredSize.Top := ReadIntegerWithDefault('RestoredTop', FormRestoredSize.Top); 132 FormRestoredSize.Right := ReadIntegerWithDefault('RestoredWidth', FormRestoredSize.Right - FormRestoredSize.Left) 133 + FormRestoredSize.Left; 134 FormRestoredSize.Bottom := ReadIntegerWithDefault('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top) 135 + FormRestoredSize.Top; 136 // Other state 137 FormWindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(wsNormal))); 138 finally 139 Free; 140 end; 141 end; 142 143 procedure TPersistentForm.SaveToRegistry(RegistryContext: TRegistryContext); 144 begin 145 with Form, TRegistryEx.Create do 146 try 147 RootKey := RegistryContext.RootKey; 148 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True); 149 // Normal state 150 WriteInteger('NormalWidth', FormNormalSize.Right - FormNormalSize.Left); 151 WriteInteger('NormalHeight', FormNormalSize.Bottom - FormNormalSize.Top); 152 WriteInteger('NormalTop', FormNormalSize.Top); 153 WriteInteger('NormalLeft', FormNormalSize.Left); 154 // Restored state 155 WriteInteger('RestoredWidth', FormRestoredSize.Right - FormRestoredSize.Left); 156 WriteInteger('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top); 157 WriteInteger('RestoredTop', FormRestoredSize.Top); 158 WriteInteger('RestoredLeft', FormRestoredSize.Left); 159 // Other state 160 WriteInteger('WindowState', Integer(FormWindowState)); 161 finally 162 Free; 163 end; 164 end; 165 46 166 function TPersistentForm.CheckEntireVisible(Rect: TRect): TRect; 47 167 var … … 98 218 procedure TPersistentForm.Load(Form: TForm; DefaultMaximized: Boolean = False); 99 219 var 100 Normal: TRect;101 Restored: TRect;102 220 LoadDefaults: Boolean; 103 221 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; 222 Self.Form := Form; 223 // Set default 224 FormNormalSize := Bounds((Screen.Width - Form.Width) div 2, 225 (Screen.Height - Form.Height) div 2, Form.Width, Form.Height); 226 FormRestoredSize := Bounds((Screen.Width - Form.Width) div 2, 227 (Screen.Height - Form.Height) div 2, Form.Width, Form.Height); 228 229 LoadFromRegistry(RegistryContext); 230 231 if not EqualRect(FormNormalSize, FormRestoredSize) or 232 (LoadDefaults and DefaultMaximized) then begin 233 // Restore to maximized state 234 Form.WindowState := wsNormal; 235 if not EqualRect(FormRestoredSize, Form.BoundsRect) then 236 Form.BoundsRect := FormRestoredSize; 237 Form.WindowState := wsMaximized; 238 end else begin 239 // Restore to normal state 240 Form.WindowState := wsNormal; 241 if FEntireVisible then FormNormalSize := CheckEntireVisible(FormNormalSize) 242 else if FMinVisiblePart > 0 then 243 FormNormalSize := CheckPartVisible(FormNormalSize, FMinVisiblePart); 244 if not EqualRect(FormNormalSize, Form.BoundsRect) then 245 Form.BoundsRect := FormNormalSize; 246 end; 247 LoadControl(Form); 144 248 end; 145 249 146 250 procedure TPersistentForm.Save(Form: TForm); 147 251 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; 252 Self.Form := Form; 253 FormNormalSize := Bounds(Form.Left, Form.Top, Form.Width, Form.Height); 254 FormRestoredSize := Bounds(Form.RestoredLeft, Form.RestoredTop, Form.RestoredWidth, 255 Form.RestoredHeight); 256 FormWindowState := Form.WindowState; 257 SaveToRegistry(RegistryContext); 258 SaveControl(Form); 165 259 end; 166 260 … … 168 262 begin 169 263 inherited; 264 if AOwner is TForm then Form := TForm(AOwner) 265 else Form := nil; 170 266 FMinVisiblePart := 50; 171 267 FRegistryContext.RootKey := HKEY_CURRENT_USER;
Note:
See TracChangeset
for help on using the changeset viewer.