Changeset 475 for Common


Ignore:
Timestamp:
Apr 18, 2015, 2:28:02 PM (10 years ago)
Author:
chronos
Message:
  • Modified: Separate Load/Save function from Registry operations in UPersistentForm unit.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/UPersistentForm.pas

    r463 r475  
    33{$mode delphi}
    44
    5 // Date: 2010-06-01
     5// Date: 2015-04-18
    66
    77interface
     
    2020    FRegistryContext: TRegistryContext;
    2121  public
     22    FormNormalSize: TRect;
     23    FormRestoredSize: TRect;
     24    FormWindowState: TWindowState;
     25    Form: TForm;
     26    procedure LoadFromRegistry(RegistryContext: TRegistryContext);
     27    procedure SaveToRegistry(RegistryContext: TRegistryContext);
    2228    function CheckEntireVisible(Rect: TRect): TRect;
    2329    function CheckPartVisible(Rect: TRect; Part: Integer): TRect;
     
    4046begin
    4147  RegisterComponents('Common', [TPersistentForm]);
     48  Screen.Desk;
    4249end;
    4350
    4451{ TPersistentForm }
     52
     53procedure TPersistentForm.LoadFromRegistry(RegistryContext: TRegistryContext);
     54begin
     55  with TRegistryEx.Create do
     56  try
     57    RootKey := RegistryContext.RootKey;
     58    OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True);
     59    // Normal size
     60    FormNormalSize.Left := ReadIntegerWithDefault('NormalLeft', FormNormalSize.Left);
     61    FormNormalSize.Top := ReadIntegerWithDefault('NormalTop', FormNormalSize.Top);
     62    FormNormalSize.Right := ReadIntegerWithDefault('NormalWidth', FormNormalSize.Right - FormNormalSize.Left)
     63      + FormNormalSize.Left;
     64    FormNormalSize.Bottom := ReadIntegerWithDefault('NormalHeight', FormNormalSize.Bottom - FormNormalSize.Top)
     65      + FormNormalSize.Top;
     66    // Restored size
     67    FormRestoredSize.Left := ReadIntegerWithDefault('RestoredLeft', FormRestoredSize.Left);
     68    FormRestoredSize.Top := ReadIntegerWithDefault('RestoredTop', FormRestoredSize.Top);
     69    FormRestoredSize.Right := ReadIntegerWithDefault('RestoredWidth', FormRestoredSize.Right - FormRestoredSize.Left)
     70      + FormRestoredSize.Left;
     71    FormRestoredSize.Bottom := ReadIntegerWithDefault('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top)
     72      + FormRestoredSize.Top;
     73    // Other state
     74    FormWindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(wsNormal)));
     75  finally
     76    Free;
     77  end;
     78end;
     79
     80procedure TPersistentForm.SaveToRegistry(RegistryContext: TRegistryContext);
     81begin
     82  with Form, TRegistryEx.Create do
     83  try
     84    RootKey := RegistryContext.RootKey;
     85    OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True);
     86    // Normal state
     87    WriteInteger('NormalWidth', FormNormalSize.Right - FormNormalSize.Left);
     88    WriteInteger('NormalHeight', FormNormalSize.Bottom - FormNormalSize.Top);
     89    WriteInteger('NormalTop', FormNormalSize.Top);
     90    WriteInteger('NormalLeft', FormNormalSize.Left);
     91    // Restored state
     92    WriteInteger('RestoredWidth', FormRestoredSize.Right - FormRestoredSize.Left);
     93    WriteInteger('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top);
     94    WriteInteger('RestoredTop', FormRestoredSize.Top);
     95    WriteInteger('RestoredLeft', FormRestoredSize.Left);
     96    // Other state
     97    WriteInteger('WindowState', Integer(FormWindowState));
     98  finally
     99    Free;
     100  end;
     101end;
    45102
    46103function TPersistentForm.CheckEntireVisible(Rect: TRect): TRect;
     
    98155procedure TPersistentForm.Load(Form: TForm; DefaultMaximized: Boolean = False);
    99156var
    100   Normal: TRect;
    101   Restored: TRect;
    102157  LoadDefaults: Boolean;
    103158begin
    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;
     159  Self.Form := Form;
     160  // Set default
     161  FormNormalSize := Bounds((Screen.Width - Form.Width) div 2,
     162    (Screen.Height - Form.Height) div 2, Form.Width, Form.Height);
     163  FormRestoredSize := Bounds((Screen.Width - Form.Width) div 2,
     164    (Screen.Height - Form.Height) div 2, Form.Width, Form.Height);
     165
     166  LoadFromRegistry(RegistryContext);
     167
     168  if not EqualRect(FormNormalSize, FormRestoredSize) or
     169    (LoadDefaults and DefaultMaximized) then begin
     170    // Restore to maximized state
     171    Form.WindowState := wsNormal;
     172    if not EqualRect(FormRestoredSize, Form.BoundsRect) then
     173      Form.BoundsRect := FormRestoredSize;
     174    Form.WindowState := wsMaximized;
     175  end else begin
     176    // Restore to normal state
     177    Form.WindowState := wsNormal;
     178    if FEntireVisible then FormNormalSize := CheckEntireVisible(FormNormalSize)
     179      else if FMinVisiblePart > 0 then
     180    FormNormalSize := CheckPartVisible(FormNormalSize, FMinVisiblePart);
     181    if not EqualRect(FormNormalSize, Form.BoundsRect) then
     182      Form.BoundsRect := FormNormalSize;
     183  end;
    144184end;
    145185
    146186procedure TPersistentForm.Save(Form: TForm);
    147187begin
    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;
     188  Self.Form := Form;
     189  FormNormalSize := Bounds(Form.Left, Form.Top, Form.Width, Form.Height);
     190  FormRestoredSize := Bounds(Form.RestoredLeft, Form.RestoredTop, Form.RestoredWidth,
     191    Form.RestoredHeight);
     192  FormWindowState := Form.WindowState;
     193  SaveToRegistry(RegistryContext);
    165194end;
    166195
     
    168197begin
    169198  inherited;
     199  if AOwner is TForm then Form := TForm(AOwner)
     200    else Form := nil;
    170201  FMinVisiblePart := 50;
    171202  FRegistryContext.RootKey := HKEY_CURRENT_USER;
Note: See TracChangeset for help on using the changeset viewer.