Changeset 21 for trunk/Packages/Common/PersistentForm.pas
- Timestamp:
- Apr 3, 2025, 10:49:00 PM (13 days ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/PersistentForm.pas
r20 r21 1 unit UPersistentForm; 2 3 {$mode delphi} 4 5 // Date: 2015-04-18 1 unit PersistentForm; 6 2 7 3 interface 8 4 9 5 uses 10 Classes, SysUtils, Forms, URegistry, LCLIntf, Registry, Controls, ComCtrls; 6 Classes, SysUtils, Forms, RegistryEx, LCLIntf, Registry, Controls, ComCtrls, 7 ExtCtrls, LCLType; 11 8 12 9 type … … 19 16 FMinVisiblePart: Integer; 20 17 FRegistryContext: TRegistryContext; 18 FResizeEventOccured: Boolean; 21 19 procedure LoadControl(Control: TControl); 22 20 procedure SaveControl(Control: TControl); 21 procedure WindowStateChange(Sender: TObject); 23 22 public 24 FormNormalSize: TRect;25 23 FormRestoredSize: TRect; 26 24 FormWindowState: TWindowState; 25 FormFullScreen: Boolean; 27 26 Form: TForm; 28 27 procedure LoadFromRegistry(RegistryContext: TRegistryContext); … … 30 29 function CheckEntireVisible(Rect: TRect): TRect; 31 30 function CheckPartVisible(Rect: TRect; Part: Integer): TRect; 32 procedure Load(Form: TForm; DefaultMaximized: Boolean = False); 31 procedure Load(Form: TForm; DefaultMaximized: Boolean = False; 32 DefaultFullScreen: Boolean = False); 33 33 procedure Save(Form: TForm); 34 34 constructor Create(AOwner: TComponent); override; 35 procedure SetFullScreen(State: Boolean); 35 36 property RegistryContext: TRegistryContext read FRegistryContext 36 37 write FRegistryContext; … … 42 43 procedure Register; 43 44 45 44 46 implementation 45 46 47 47 48 procedure Register; … … 56 57 I: Integer; 57 58 WinControl: TWinControl; 58 Count: Integer;59 59 begin 60 60 if Control is TListView then begin … … 72 72 end; 73 73 74 if (Control is TPanel) then begin 75 with Form, TRegistryEx.Create do 76 try 77 RootKey := RegistryContext.RootKey; 78 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name + '\' + Control.Name, True); 79 if (TPanel(Control).Align = alRight) or (TPanel(Control).Align = alLeft) then begin 80 if ValueExists('Width') then 81 TPanel(Control).Width := ReadInteger('Width'); 82 end; 83 if (TPanel(Control).Align = alTop) or (TPanel(Control).Align = alBottom) then begin 84 if ValueExists('Height') then 85 TPanel(Control).Height := ReadInteger('Height'); 86 end; 87 finally 88 Free; 89 end; 90 end; 91 74 92 if Control is TWinControl then begin 75 93 WinControl := TWinControl(Control); … … 96 114 for I := 0 to TListView(Control).Columns.Count - 1 do begin 97 115 WriteInteger('ColWidth' + IntToStr(I), TListView(Control).Columns[I].Width); 116 end; 117 finally 118 Free; 119 end; 120 end; 121 122 if (Control is TPanel) then begin 123 with Form, TRegistryEx.Create do 124 try 125 RootKey := RegistryContext.RootKey; 126 OpenKey(RegistryContext.Key + '\Forms\' + Form.Name + '\' + Control.Name, True); 127 if (TPanel(Control).Align = alRight) or (TPanel(Control).Align = alLeft) then begin 128 WriteInteger('Width', TPanel(Control).Width); 129 end; 130 if (TPanel(Control).Align = alTop) or (TPanel(Control).Align = alBottom) then begin 131 WriteInteger('Height', TPanel(Control).Height); 98 132 end; 99 133 finally … … 120 154 RootKey := RegistryContext.RootKey; 121 155 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; 156 129 157 // Restored size 130 158 FormRestoredSize.Left := ReadIntegerWithDefault('RestoredLeft', FormRestoredSize.Left); … … 134 162 FormRestoredSize.Bottom := ReadIntegerWithDefault('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top) 135 163 + FormRestoredSize.Top; 164 136 165 // Other state 137 FormWindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(wsNormal))); 166 FormWindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(FormWindowState))); 167 FormFullScreen := ReadBoolWithDefault('FullScreen', FormFullScreen); 138 168 finally 139 169 Free; … … 147 177 RootKey := RegistryContext.RootKey; 148 178 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 179 180 // Restored size 155 181 WriteInteger('RestoredWidth', FormRestoredSize.Right - FormRestoredSize.Left); 156 182 WriteInteger('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top); 157 183 WriteInteger('RestoredTop', FormRestoredSize.Top); 158 184 WriteInteger('RestoredLeft', FormRestoredSize.Left); 185 159 186 // Other state 160 187 WriteInteger('WindowState', Integer(FormWindowState)); 188 WriteBool('FullScreen', FormFullScreen); 161 189 finally 162 190 Free; … … 216 244 end; 217 245 218 procedure TPersistentForm.Load(Form: TForm; DefaultMaximized: Boolean = False); 219 var 220 LoadDefaults: Boolean; 246 procedure TPersistentForm.Load(Form: TForm; DefaultMaximized: Boolean = False; 247 DefaultFullScreen: Boolean = False); 221 248 begin 222 249 Self.Form := Form; 250 223 251 // Set default 224 FormNormalSize := Bounds((Screen.Width - Form.Width) div 2,225 (Screen.Height - Form.Height) div 2, Form.Width, Form.Height);226 252 FormRestoredSize := Bounds((Screen.Width - Form.Width) div 2, 227 253 (Screen.Height - Form.Height) div 2, Form.Width, Form.Height); 254 FormWindowState := Form.WindowState; 255 FormFullScreen := DefaultFullScreen; 228 256 229 257 LoadFromRegistry(RegistryContext); 230 258 231 if not EqualRect(FormNormalSize, FormRestoredSize) or 232 (LoadDefaults and DefaultMaximized) then begin 259 if (FormWindowState = wsMaximized) or DefaultMaximized then begin 233 260 // Restore to maximized state 234 261 Form.WindowState := wsNormal; … … 239 266 // Restore to normal state 240 267 Form.WindowState := wsNormal; 241 if FEntireVisible then Form NormalSize := CheckEntireVisible(FormNormalSize)268 if FEntireVisible then FormRestoredSize := CheckEntireVisible(FormRestoredSize) 242 269 else if FMinVisiblePart > 0 then 243 FormNormalSize := CheckPartVisible(FormNormalSize, FMinVisiblePart); 244 if not EqualRect(FormNormalSize, Form.BoundsRect) then 245 Form.BoundsRect := FormNormalSize; 246 end; 270 FormRestoredSize := CheckPartVisible(FormRestoredSize, FMinVisiblePart); 271 if not EqualRect(FormRestoredSize, Form.BoundsRect) then 272 Form.BoundsRect := FormRestoredSize; 273 end; 274 if FormFullScreen then SetFullScreen(True); 247 275 LoadControl(Form); 248 276 end; … … 251 279 begin 252 280 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; 281 if not FormFullScreen then begin 282 FormWindowState := Form.WindowState; 283 if FormWindowState = wsMaximized then begin 284 FormRestoredSize := Bounds(Form.RestoredLeft, Form.RestoredTop, Form.RestoredWidth, 285 Form.RestoredHeight); 286 end else 287 if FormWindowState = wsNormal then begin 288 FormRestoredSize := Bounds(Form.Left, Form.Top, Form.Width, Form.Height); 289 end; 290 end; 257 291 SaveToRegistry(RegistryContext); 258 292 SaveControl(Form); … … 268 302 end; 269 303 304 procedure TPersistentForm.SetFullScreen(State: Boolean); 305 {$IFDEF UNIX} 306 var 307 OldHandler: TNotifyEvent; 308 var 309 I: Integer; 310 {$ENDIF} 311 begin 312 if State then begin 313 FormFullScreen := True; 314 if Form.WindowState = wsMaximized then begin 315 FormRestoredSize := Bounds(Form.RestoredLeft, Form.RestoredTop, Form.RestoredWidth, 316 Form.RestoredHeight); 317 end else 318 if Form.WindowState = wsNormal then begin 319 FormRestoredSize := Bounds(Form.Left, Form.Top, Form.Width, Form.Height); 320 end; 321 FormWindowState := Form.WindowState; 322 {$IFDEF WINDOWS} 323 Form.BorderStyle := bsNone; 324 {$ENDIF} 325 Form.WindowState := wsFullscreen; 326 {$IFDEF UNIX} 327 // Workaround on Linux, WindowState is rewriten by WMSize event to wsNormal. 328 // We need for that even to occure 329 OldHandler := Form.OnWindowStateChange; 330 Form.OnWindowStateChange := WindowStateChange; 331 FResizeEventOccured := False; 332 for I := 0 to 10 do begin 333 if FResizeEventOccured then Break; 334 Application.ProcessMessages; 335 Sleep(1); 336 end; 337 Form.OnWindowStateChange := OldHandler; 338 FormFullScreen := True; 339 {$ENDIF} 340 end else begin 341 FormFullScreen := False; 342 Form.WindowState := wsNormal; 343 {$IFDEF WINDOWS} 344 Form.BorderStyle := bsSizeable; 345 {$ENDIF} 346 if FormWindowState = wsNormal then begin 347 Form.WindowState := wsNormal; 348 Form.BoundsRect := FormRestoredSize; 349 end else 350 if FormWindowState = wsMaximized then begin 351 Form.BoundsRect := FormRestoredSize; 352 Form.WindowState := wsMaximized; 353 end; 354 end; 355 end; 356 357 procedure TPersistentForm.WindowStateChange(Sender: TObject); 358 begin 359 Form.WindowState := wsFullscreen; 360 FResizeEventOccured := True; 361 end; 362 270 363 end. 271
Note:
See TracChangeset
for help on using the changeset viewer.