unit RememberPos;

interface

uses
  SysUtils, Classes, Registry, Forms, Windows;

type
  THKEY = (CURRENT_USER, LOCAL_MACHINE);
  TRememberPos = class(TComponent)
  private
    FRootKey: THKEY;
    FRegLocation: String;
    ParentName: String;
    Owner: TComponent;
    FRegRootKey: THKEY;
    Function THKEYToHKEY(Key: THKEY): HKEY;
    procedure SetRegLocation(const Value: String);
    procedure SetRegRootKey(const Value: THKEY);
    { Private declarations }
  protected
    { Protected declarations }
  public
    Constructor Create(aOwner: TComponent); override;
    Destructor Destroy; override;
  published
    Property RegRootKey: THKEY read FRegRootKey write SetRegRootKey;
    Property RegLocation: String read FRegLocation write SetRegLocation;
    Procedure Reload;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('LPL Tools', [TRememberPos]);
end;

{ TRememberPos }

constructor TRememberPos.Create(aOwner: TComponent);
begin
 inherited;
 if (csDesigning in ComponentState) then exit;
 Owner := aOwner;
 If not (Owner is TForm) then exit;
 ParentName := (Owner as TForm).Name;
 Reload;
end;

destructor TRememberPos.Destroy;
Var
 Reg: TRegistry;
// WS: Integer;
begin
 If (Owner is TForm) then begin
//  case (owner as TForm).WindowState of
//   wsNormal: WS := 0;
//   wsMaximized: WS := 1;
//   else WS := 2;
//  end;
  Reg := TRegistry.Create;
  Reg.RootKey := THkeyToHkey(fRootKey);
  If Reg.OpenKey(fRegLocation, true) then begin
   Reg.WriteInteger(ParentName + 'L', (owner as TForm).Left);
   Reg.WriteInteger(ParentName + 'T', (owner as TForm).Top);
   Reg.WriteInteger(ParentName + 'H', (owner as TForm).Height);
   Reg.WriteInteger(ParentName + 'W', (owner as TForm).Width);
//   Reg.WriteInteger(ParentName + '_WS', WS);
  End;
  FreeAndNil(Reg);
 End;
 inherited;
end;

procedure TRememberPos.Reload;
Var
 Reg: Tregistry;
// WS: Integer;
begin
 Reg := TRegistry.Create;
 Reg.RootKey := THKEYtoHKEY(fRootKey);
 If Reg.OpenKey(fRegLocation, false) then begin
  If Reg.ValueExists(ParentName + 'L') then
   (owner as TForm).Left := reg.ReadInteger(ParentName + 'L');
  If Reg.ValueExists(ParentName + 'T') then
   (owner as Tform).Top := reg.ReadInteger(ParentName + 'T');
  If Reg.ValueExists(ParentName + 'H') then
   (owner as Tform).Height := reg.ReadInteger(ParentName + 'H');
  If Reg.ValueExists(ParentName + 'W') then
   (owner as Tform).Width := reg.ReadInteger(ParentName + 'W');
{  if Reg.ValueExists(ParentNAme + '_WS') then begin
   WS := Reg.ReadInteger(ParentName + '_WS');
   case WS of
    0: (owner as TForm).WindowState := wsNormal;
    1: (owner as TForm).WindowState := wsMaximized;
    else (owner as TForm).WindowState := wsMinimized;
   end;
  end; }
 (owner as Tform).Update;
 End;
 FreeAndNil(Reg);
end;

procedure TRememberPos.SetRegLocation(const Value: String);
begin
  FRegLocation := Value;
end;

procedure TRememberPos.SetRegRootKey(const Value: THKEY);
begin
  FRegRootKey := Value;
end;

function TRememberPos.THKEYToHKEY(Key: THKEY): HKEY;
begin
 Result := HKEY_CURRENT_USER;
 Case Key of
  Current_User: Result := HKEY_CURRENT_USER;
  Local_Machine: Result := HKEY_LOCAL_MACHINE;
 end;
end;

end.
