Changeset 109 for trunk/Packages/Common/UPersistentForm.pas
- Timestamp:
- Jul 26, 2016, 11:22:43 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/UPersistentForm.pas
r43 r109 3 3 {$mode delphi} 4 4 5 // Date: 201 0-06-015 // Date: 2015-04-18 6 6 7 7 interface … … 20 20 FRegistryContext: TRegistryContext; 21 21 public 22 FormNormalSize: TRect; 23 FormRestoredSize: TRect; 24 FormWindowState: TWindowState; 25 Form: TForm; 26 procedure LoadFromRegistry(RegistryContext: TRegistryContext); 27 procedure SaveToRegistry(RegistryContext: TRegistryContext); 22 28 function CheckEntireVisible(Rect: TRect): TRect; 23 29 function CheckPartVisible(Rect: TRect; Part: Integer): TRect; … … 44 50 { TPersistentForm } 45 51 52 procedure TPersistentForm.LoadFromRegistry(RegistryContext: TRegistryContext); 53 begin 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; 77 end; 78 79 procedure TPersistentForm.SaveToRegistry(RegistryContext: TRegistryContext); 80 begin 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; 100 end; 101 46 102 function TPersistentForm.CheckEntireVisible(Rect: TRect): TRect; 47 103 var … … 98 154 procedure TPersistentForm.Load(Form: TForm; DefaultMaximized: Boolean = False); 99 155 var 100 Normal: TRect;101 Restored: TRect;102 156 LoadDefaults: Boolean; 103 157 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; 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; 144 183 end; 145 184 146 185 procedure TPersistentForm.Save(Form: TForm); 147 186 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; 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); 165 193 end; 166 194 … … 168 196 begin 169 197 inherited; 198 if AOwner is TForm then Form := TForm(AOwner) 199 else Form := nil; 170 200 FMinVisiblePart := 50; 171 201 FRegistryContext.RootKey := HKEY_CURRENT_USER;
Note:
See TracChangeset
for help on using the changeset viewer.