Changeset 28
- Timestamp:
- Jun 4, 2010, 6:51:31 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
PersistentForm/UPersistentForm.pas
r26 r28 3 3 {$mode delphi} 4 4 5 // Date: 2010-06-01 6 5 7 interface 6 8 7 9 uses 8 Classes, SysUtils, Forms, URegistry, Windows;10 Classes, SysUtils, Forms, URegistry, LCLIntf, Registry; 9 11 10 12 type … … 15 17 public 16 18 RegistryKey: string; 17 RegistryRootKey: Cardinal;19 RegistryRootKey: HKEY; 18 20 procedure Load(Form: TForm); 19 21 procedure Save(Form: TForm); … … 27 29 procedure TPersistentForm.Load(Form: TForm); 28 30 var 29 Pl : TWindowPlacement; // used for API call 30 R : TRect; // used for wdw pos 31 Width, Height, Top, Left : Integer; 31 RestoredLeft, RestoredTop, RestoredWidth, RestoredHeight: Integer; 32 32 begin 33 33 with TRegistryEx.Create do … … 36 36 OpenKey (RegistryKey + '\Forms\' + Form.Name, True); 37 37 38 Width := ReadIntegerWithDefault ('Width', Form.Width); 39 Height := ReadIntegerWithDefault ('Height', Form.Height); 40 Top := ReadIntegerWithDefault ('Top', (Screen.Height - Form.Height) div 2); 41 Left := ReadIntegerWithDefault ('Left', (Screen.Width - Form.Width) div 2); 42 if Left < 0 then 43 Left := 0; 44 if Left > (Screen.Width - 50) then 45 Left := Screen.Width - 50; 46 if Top < 0 then 47 Top := 0; 48 if Top > (Screen.Height - 50) then 49 Top := Screen.Height - 50; 38 Form.Width := ReadIntegerWithDefault('Width', Form.Width); 39 Form.Height := ReadIntegerWithDefault('Height', Form.Height); 40 Form.Top := ReadIntegerWithDefault('Top', (Screen.Height - Form.Height) div 2); 41 Form.Left := ReadIntegerWithDefault('Left', (Screen.Width - Form.Width) div 2); 42 if Form.Left < 0 then 43 Form.Left := 0; 44 if Form.Left > (Screen.Width - 50) then 45 Form.Left := Screen.Width - 50; 46 if Form.Top < 0 then 47 Form.Top := 0; 48 if Form.Top > (Screen.Height - 50) then 49 Form.Top := Screen.Height - 50; 50 RestoredWidth := ReadIntegerWithDefault('RestoredWidth', Form.RestoredWidth); 51 RestoredHeight := ReadIntegerWithDefault ('RestoredHeight', Form.RestoredHeight); 52 RestoredTop := ReadIntegerWithDefault ('RestoredTop', (Screen.Height - Form.RestoredHeight) div 2); 53 RestoredLeft := ReadIntegerWithDefault ('RestoredLeft', (Screen.Width - Form.RestoredWidth) div 2); 54 Form.SetRestoredBounds(RestoredLeft, RestoredTop, RestoredWidth, RestoredHeight); 50 55 51 // Restore position using WinAPI 52 Pl.Length := SizeOf (TWindowPlacement); 53 GetWindowPlacement (Form.Handle, @Pl); 54 R := Pl.rcNormalPosition; 55 R.Left := Left; 56 R.Top := Top; 57 R.Right := Left + Width; 58 R.Bottom := Top + Height; 59 Pl.rcNormalPosition := R; 60 if ReadBoolWithDefault('Maximized', False) then 61 Pl.showCmd := SW_SHOWMAXIMIZED else Pl.showCmd := SW_HIDE; 62 SetWindowPlacement(Form.Handle, @Pl); 63 56 Form.WindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(wsNormal))); 64 57 if ReadBoolWithDefault('Visible', False) then Form.Show; 65 58 finally … … 69 62 70 63 procedure TPersistentForm.Save(Form: TForm); 71 var72 Pl: TWindowPlacement; // used for API call73 R: TRect; // used for wdw pos74 64 begin 75 {Calculate window's normal size and position using76 Windows API call - the form's Width, Height, Top and77 Left properties will give maximized window size if78 form is maximised, which is not what we want here}79 Pl.Length := SizeOf (TWindowPlacement);80 GetWindowPlacement (Form.Handle, @Pl);81 R := Pl.rcNormalPosition;82 83 65 with Form, TRegistryEx.Create do 84 66 try 85 67 RootKey := RegistryRootKey; 86 68 OpenKey(RegistryKey + '\Forms\' + Form.Name, True); 87 WriteInteger('Width', R.Right - R.Left); 88 WriteInteger('Height', R.Bottom - R.Top); 89 WriteInteger('Top', R.Top); 90 WriteInteger('Left', R.Left); 91 WriteBool('Maximized', WindowState = wsMaximized); 69 WriteInteger('Width', Form.Width); 70 WriteInteger('Height', Form.Height); 71 WriteInteger('Top', Form.Top); 72 WriteInteger('Left', Form.Left); 73 WriteInteger('RestoredWidth', Form.RestoredWidth); 74 WriteInteger('RestoredHeight', Form.RestoredHeight); 75 WriteInteger('RestoredTop', Form.RestoredTop); 76 WriteInteger('RestoredLeft', Form.RestoredLeft); 77 WriteInteger('WindowState', Integer(Form.WindowState)); 92 78 WriteBool('Visible', Form.Visible); 93 79 finally
Note:
See TracChangeset
for help on using the changeset viewer.