2006. február 12., vasárnap

How to save/restore the form state in/from the registry


Problem/Question/Abstract:

How to save/restore the form state in/from the registry

Answer:

You may try the following unit. There is one thing you may have to handle separately somehow: the mainform is never actually minimized, so its Windowstate is never wsMinimized, unless you set it in code. When you minimize the main form it is hidden and the Application window is minimized instead. You can check whether it is minimized via

if IsIconic(Application.handle) then
  {... app is minimized}

unit RegWinset;

interface

uses
  Registry, Forms;

procedure SaveWindowstate(ini: TRegInifile; form: TForm);
procedure RestoreWindowstate(ini: TRegInifile; form: TForm);
procedure SaveWindowstateEx(ini: TRegInifile; form: TForm; const section: string);
procedure RestoreWindowstateEx(ini: TRegInifile; form: TForm; const section: string);

implementation

uses
  TypeInfo, Windows;

const
  sSettings = 'Settings';
  sLeft = 'Left';
  sTop = 'Top';
  sWidth = 'Width';
  sHeight = 'Height';
  sState = 'State';

  {Procedure SaveWindowStateEx;

  Parameters:
  ini: inifile to save the settings in
  form: form to save the settings for
  section: section to use for the settings

  Call method: static
  Description: Saves the windows position and size to the INI file.
  Error Conditions: none
  Created: 03.07.97 16:34:34 by P. Below}

procedure SaveWindowstateEx(ini: TRegInifile; form: TForm; const section: string);
var
  wp: TWindowPlacement;
begin
  wp.length := Sizeof(wp);
  GetWindowPlacement(form.handle, @wp);
  with Ini, wp.rcNormalPosition do
  begin
    WriteInteger(section, sLeft, Left);
    WriteInteger(section, sTop, Top);
    WriteInteger(section, sWidth, Right - Left);
    WriteInteger(section, sHeight, Bottom - Top);
    WriteString(section, sState, GetEnumName(TypeInfo(TWindowState),
      Ord(form.WindowState));
  end;
end;

{Procedure RestoreWindowStateEx;

Parameters:
ini: inifile to restore the settings from
form: form to restore the settings for
section: section to use for the settings

Call method: static

Description:
Restores the window position and dimensions from the saved values in the INI file.
If there ain't any, nothing changes.

Error Conditions: none

Created: 03.07.97 16:33:27 by P. Below}

procedure RestoreWindowstateEx(ini: TRegInifile; form: TForm; const section: string);
var
  L, T, W, H: Integer;
begin
  with Ini, form do
  begin
    L := ReadInteger(section, sLeft, Left);
    T := ReadInteger(section, sTop, Top);
    W := ReadInteger(section, sWidth, Width);
    H := ReadInteger(section, sHeight, Height);
    SetBounds(L, T, W, H);
    try
      Windowstate := TWindowState(GetEnumValue(TypeInfo(TWindowState),
        ReadString(section, sState, 'wsNormal')));
    except
    end;
  end;
end;

{ Save state using default settings section }

procedure SaveWindowstate(ini: TRegInifile; form: TForm);
begin
  SaveWindowstateEx(ini, form, sSettings);
end;

{ Restore state using default settings section }

procedure RestoreWindowstate(ini: TRegInifile; form: TForm);
begin
  RestoreWindowStateEx(ini, form, sSettings);
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése