Forms Delphi

Title: VCL: Center a form over active form
Question: How to center a form (e.g. messagebox) over active form
Answer:
procedure CenterFormOverActive (AForm: TForm);
var ALeft, ATop: integer;
begin
ALeft := Screen.ActiveForm.Left +
(Screen.ActiveForm.Width div 2) - (AForm.Width div 2);
ATop := Screen.ActiveForm.Top +
(Screen.ActiveForm.Height div 2) - (AForm.Height div 2);
{ prevent form from being outside screen }
if ALeft ALeft := Screen.ActiveForm.Left;
if ATop ATop := Screen.ActiveForm.Top;
if (ALeft + AForm.Width Screen.Width) or
(ATop + AForm.Height Screen.Height)
then
CenterForm (AForm)
else
{ prevents form being twice repainted! }
AForm.SetBounds (ALeft, ATop, AForm.Width, AForm.Height);
end;