DelphiFAQ Home Search:

Center a Form efficiently

 

comments2 comments. Current rating: 5 stars (1 votes). Leave comments and/ or rate it.

To center a form after having changed its dimensions at run-time,
the poScreenCenter won't do it - it only works when the form is shown.

The following code shows 2 solutions how to handle this "problem":

  // this works, but the form will be redrawn two times
  // (one redraw for each assignment)
  Form1.Left := (Screen.Width  div 2) - (Form.Width div 2);
  Form1.Top := (Screen.Height div 2) - (Form.Height div 2);

  // this is better.. the form is redrawn only once
  Form1.SetBounds((Screen.Width - AForm.Width) div 2,
                  (Screen.Height - AForm.Height) div 2,
                   Form1.Width, Form1.Height);

Comments:

2005-12-21, 00:44:17
anonymous from United States  
rating
There is an extra parameter in this. Just delete ATop and now you have the four parameters and it works great.

Thank you.
2005-12-21, 12:26:54
Peter (Admin) from United States  
@anonymous:
Thanks, the code is corrected. I think I posted this snippet about 5 or 6 years ago - in 1999 or 2000 - and nobody said anything until now. I finally got to correct it!
-Peter

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Los Angeles, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.