Delphi 7: How to get a form exactly centered on top of another form

PTRUSCOTT
PTRUSCOTT used Ask the Experts™
on
Dear Experts,
I'm using Delphi 7.  I have an application where I want various dialog boxes pop up inside the form of the main application.  I want to be able to read the position of the main form and then set the upper left hand corner position of the inner dialog box so it is perfectly centered inside the form of the main application.  Can someone provide a code fragment please.
Sincerely,
Philip T.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
For example, Form2 is a form that will be centered and Form1 is main form:

Form2.Left:=Form1.Left+Round((Form1.Width-Form2.Width)/2);
Form2.Top:=Form1.Top+Round((Form1.Height-Form2.Height)/2);

Regards,
B.
Set the child's form (dialogbox) position property to poMainFormCenter, this will do the trick
Geert GOracle dba
Top Expert 2009

Commented:
procedure TForm1.Button1Click(Sender: TObject);
begin
  CallForm2(Self);
end;

// set property of Form2 for position = poOwnerFormCenter

procedure CallForm2(AOwner: TComponent);
var frm: TForm2;
begin
  frm := TForm2.Create(AOwner);
  try
    frm. // etc
  finally
    FreeAndNil(frm);
  end;
end;

this works for every form called the next level too

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial