Mirror

Center a Form efficiently (Views: 710)


Problem/Question/Abstract:

Center a Form efficiently

Answer:

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,
  ATop, Form1.Width, Form1.Height);

<< Back to main page