Link to home
Start Free TrialLog in
Avatar of Ed Covney
Ed CovneyFlag for United States of America

asked on

Application Window Placement

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
ASKER CERTIFIED SOLUTION
Avatar of VahaC
VahaC
Flag of Ukraine image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ephraim Wangoya

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;
Avatar of Ed Covney

ASKER

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.