Application Window Placement

Ed Covney
Ed Covney used Ask the Experts™
on
When a user closes my app, I'd like to have it open at exactly the same location where it last closed - including a second monitor (if used), ie, exactly the way Notepad.exe behaves.

I already use a startup cfg file for other purposes but I don't know how to capture the (X,Y) location of the app before exiting.

TIA
ED
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
I use this code

procedure SaveFormStateToReg(aForm: TForm);
var
  Reg: TRegistry;
  AppTitle: String;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    AppTitle := Application.Title;
    if AppTitle = '' then
      AppTitle := 'My application';
    Reg.OpenKey('\Software\' + AppTitle + '\' + aForm.Name, True);
    with aForm do
    begin
      Reg.WriteInteger('WindowState', Integer(WindowState));
      Reg.WriteInteger('Left', Left);
      Reg.WriteInteger('Top', Top);
      Reg.WriteInteger('Width', Width);
      Reg.WriteInteger('Height', Height);
    end;
  finally
    Reg.Free;
  end;
end;
 
procedure LoadFormStateFromReg(aForm: TForm);
var
  Reg: TRegistry;
  AppTitle: String;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    AppTitle := Application.Title;
    if AppTitle = '' then
      AppTitle := 'My application';
    if Reg.OpenKeyReadOnly('\Software\' + AppTitle + '\' + aForm.Name) then
      with aForm do
      begin
        WindowState := TWindowState(Reg.ReadInteger('WindowState'));
        if WindowState = wsNormal then
        begin
          Left := Reg.ReadInteger('Left');
          Top := Reg.ReadInteger('Top');
          Width := Reg.ReadInteger('Width');
          Height := Reg.ReadInteger('Height');
        end;
      end;
  finally
    Reg.Free;
  end;
end;

Open in new window

Ephraim WangoyaSoftware Engineer

Commented:

Use the forms properties, Left and Top to store the position. You can store this in your cfg file.

When instantiating the form, in your OnShow event, read the valuse from the cfg file and just assign them to your form

TForm1.FormShow(Sender: TObject);
var
  SavedLeft, SavedTop: Integer;
begin
  //read the values from the configuration file
  ....
  Left := SavedLeft;
  Top := SavedTop;
end;

If you save the width and Height as well, you can just call SetBounds
TForm1.FormShow(Sender: TObject);
var
  SavedLeft, SavedTop, SavedWidth, SavedHeight: Integer;
begin
  //read the values from the configuration file
  ....
  SetBounds(SavedLeft, SavedTop, SavedWidth, SavedHeight);
end;
Ed CovneyRetired

Author

Commented:
VahaC - Thank you so much for sharing your code. It's perfect !!!

Ewangoya - Although your code also works perfectly, by the time I read my config file, the form is painted and then must be repositioned from the config file. You've helped me numerous times before and I always appreciate your code and willingness to share.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial