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

Avatar of undefined
Last Comment
Ed Covney

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
VahaC

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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;
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.
Your help has saved me hundreds of hours of internet surfing.
fblack61