Link to home
Start Free TrialLog in
Avatar of Kmiller
Kmiller

asked on

center a form within another form

I have small dialog/form that is started from the main form. I want this small form to always come up centered within the main form no matter where it is on the screen. How can I do this in Delphi 4.0?
ASKER CERTIFIED SOLUTION
Avatar of Matvey
Matvey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BlackMan
BlackMan

I use something like this in the FormShow method on the dialog form (where frmMain is the main form):

  (* Center dialog on mainform *)
  PWidth := frmMain.Width;
  PHeight := frmMain.Height;
  Left := ((PWidth - Width) Div 2) + frmMain.ClientOrigin.X;
  Top := ((PHeight - Height) Div 2) + frmMain.ClientOrigin.Y;

Ok Matvey, you were the fastest! But mine works better (I guess)with MDI forms!
In the main form that sits behind not the dialog form, enter this in OnShow...

procedure MainForm.OnShow......
begin
  DialogForm.Top := ( MainForm.Height div 2 ) - ( DialogForm.Height div 2 );
  DialogForm.left := ( MainForm.Width div 2 ) - ( DialogForm.Width div 2 );
end;

That's all you need to do! ;)

Regards,
Viktor Ivanov
Wow, you were very fast... ;)

Matvey did you see the comp I sent you the other day? Is it cool?

Regards,
Viktor Ivanov