This is a routine in one of my utilities units

procedure SaveFormState(AForm : TForm);
var
  Reg   : TRegistry;
  aRect : TRect;
begin
  if (AForm <> nil) and
     (length(AForm.ClassName) > 0) then begin
    Reg := TRegistry.Create;
    try
      with Reg do begin
        RootKey := UserPrefs;
        try
          if OpenKey(MaCompRegPath + RegFormsPath + '\' +
AForm.ClassName, true) then begin
            try
              aRect := AForm.BoundsRect;
              writeBinaryData(RegBounds, aRect,
sizeOf(TRect));
              writeInteger(RegWinState,
ord(AForm.WindowState));
            except
              on ERegistryException do;
            end;
          end;
        except
          on E:Exception do;
        end;
      end;
    finally
      Reg.free;
    end;
  end;
end;

and this is its partner

function ReadFormState(AForm : TForm) : boolean;
var
  Reg   : TRegistry;
  aRect : TRect;
  st    : TWindowState;

begin
  result := false;
  if AForm <> nil then begin
    LockWindowUpdate(AForm.Handle);
    Reg := TRegistry.Create;
    try
      with Reg do begin
        RootKey := UserPrefs;
        if OpenKey(MaCompRegPath + RegFormsPath + '\' +
AForm.ClassName, false) then begin
          try
            ReadBinaryData(RegBounds, aRect, sizeOf(TRect));
            try
              st := TWindowState(ReadInteger(RegWinState));
            except
              on ERegistryException do
                st := wsNormal;
            end;

            try
              if st = wsMaximized then begin
                AForm.WindowState := st;
              end else begin
                AForm.BoundsRect := aRect;
                AForm.WindowState := st;
              end;
              result := true;

            except
              on E:Exception do;
            end;

          except
            on ERegistryException do;
          end;
        end else begin
        end;
      end;
    finally
      LockWindowUpdate(0);
      Reg.free;
    end;
    if not result then begin
      AForm.Position := poScreenCenter;
      AForm.WindowState := wsNormal;
    end;
    Sleep(100);
  end;
end;

hth

Mick

_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to