Center a component within another  

Send By: Ferran Piņana
Web : N.A.
Email: ferran77@menta.net
Date: 26/12/99

Tip accessed 514 times

 


Useful for center a component with other component as reference.

In the next example, a TLabel (Label1) is centered into a TForm (Form1):


 procedure TForm1.Button1Click(Sender: TObject);

   Procedure CenterInClient(Obj:TControl; Const ObjRef:TControl);
   Begin
     Obj.Left:=(ObjRef.ClientWidth  Div 2) - (Obj.Width  Div 2);
     Obj.Top :=(ObjRef.ClientHeight Div 2) - (Obj.Height Div 2);

     If Obj.Left<0 Then Obj.Left:=0;
     If Obj.Top<0 Then Obj.Top:=0;
   End;

 begin
   CenterInClient(Label1,Form1);
 end;