Title: How to emulate the "Align" tool at run-time
Question: Never needet to use the align tool provided in the Delphi IDE at run-time? Now, it's easy and fast with this code.
Answer:
Procedure CenterControls( ComponentsList : Array Of TControl );
Var
IdX : Integer;
MinX : Integer;
MinY : Integer;
MaxX : Integer;
MaxY : Integer;
CenterX : Integer;
CenterY : Integer;
Begin
If ( High( ComponentsList ) Low( ComponentsList ) ) Then Begin
MinX := ComponentsList[ Low( ComponentsList ) ].Left;
MinY := ComponentsList[ Low( ComponentsList ) ].Top;
MaxX := ComponentsList[ Low( ComponentsList ) ].Left + ComponentsList[ Low( ComponentsList ) ].Width;
MaxY := ComponentsList[ Low( ComponentsList ) ].Top + ComponentsList[ Low( ComponentsList ) ].Height;
For IdX := Low( ComponentsList ) To High( ComponentsList ) Do Begin
If ( ComponentsList[ IdX ].Left If ( ComponentsList[ IdX ].Top If ( ( ComponentsList[ IdX ].Left + ComponentsList[ IdX ].Width ) MaxX ) Then MaxX := ComponentsList[ IdX ].Left + ComponentsList[ IdX ].Width;
If ( ( ComponentsList[ IdX ].Top + ComponentsList[ IdX ].Height ) MaxY ) Then MaxY := ComponentsList[ IdX ].Top + ComponentsList[ IdX ].Height;
End;
CenterX := ( MinX + MaxX ) Div 2;
CenterY := ( MinY + MaxY ) Div 2;
For IdX := Low( ComponentsList ) To High( ComponentsList ) Do Begin
ComponentsList[ IdX ].Left := CenterX - ( ComponentsList[ IdX ].Width Div 2 );
ComponentsList[ IdX ].Top := CenterY - ( ComponentsList[ IdX ].Height Div 2 );
End;
End;
End;
Procedure CenterInWindowControls( Window : TForm; ComponentsList : Array Of TControl );
Var
IdX : Integer;
CenterX : Integer;
CenterY : Integer;
Begin
If ( High( ComponentsList ) Low( ComponentsList ) ) Then Begin
CenterX := Window.Width Div 2;
CenterY := Window.Height Div 2;
For IdX := Low( ComponentsList ) To High( ComponentsList ) Do Begin
ComponentsList[ IdX ].Left := CenterX - ( ComponentsList[ IdX ].Width Div 2 );
ComponentsList[ IdX ].Top := CenterY - ( ComponentsList[ IdX ].Height Div 2 );
End;
End;
End;
Procedure AlignLeftControls( ComponentsList : Array Of TControl );
Var
MinX : Integer;
IdX : Integer;
Begin
If ( High( ComponentsList ) Low( ComponentsList ) ) Then Begin
MinX := ComponentsList[ Low( ComponentsList ) ].Left;
For IdX := Low( ComponentsList ) To High( ComponentsList ) Do
If ( ComponentsList[ IdX ].Left MinX := ComponentsList[ IdX ].Left;
For IdX := Low( ComponentsList ) To High( ComponentsList ) Do
ComponentsList[ IdX ].Left := MinX;
End;
End;
Procedure AlignTopControls( ComponentsList : Array Of TControl );
Var
MinY : Integer;
IdX : Integer;
Begin
If ( High( ComponentsList ) Low( ComponentsList ) ) Then Begin
MinY := ComponentsList[ Low( ComponentsList ) ].Top;
For IdX := Low( ComponentsList ) To High( ComponentsList ) Do
If ( ComponentsList[ IdX ].Top MinY := ComponentsList[ IdX ].Top;
For IdX := Low( ComponentsList ) To High( ComponentsList ) Do
ComponentsList[ IdX ].Top := MinY;
End;
End;
Procedure AlignRightControls( ComponentsList : Array Of TControl );
Var
MaxX : Integer;
IdX : Integer;
Begin
If ( High( ComponentsList ) Low( ComponentsList ) ) Then Begin
MaxX := ComponentsList[ Low( ComponentsList ) ].Left + ComponentsList[ Low( ComponentsList ) ].Width;
For IdX := Low( ComponentsList ) To High( ComponentsList ) Do
If ( ( ComponentsList[ IdX ].Left + ComponentsList[ IdX ].Width ) MaxX ) Then
MaxX := ComponentsList[ IdX ].Left + ComponentsList[ IdX ].Width;
For IdX := Low( ComponentsList ) To High( ComponentsList ) Do
ComponentsList[ IdX ].Left := MaxX - ComponentsList[ IdX ].Width;
End;
End;
Procedure AlignBottomControls( ComponentsList : Array Of TControl );
Var
MaxY : Integer;
IdX : Integer;
Begin
If ( High( ComponentsList ) Low( ComponentsList ) ) Then Begin
MaxY := ComponentsList[ Low( ComponentsList ) ].Top + ComponentsList[ Low( ComponentsList ) ].Height;
For IdX := Low( ComponentsList ) To High( ComponentsList ) Do
If ( ( ComponentsList[ IdX ].Top + ComponentsList[ IdX ].Height ) MaxY ) Then
MaxY := ComponentsList[ IdX ].Top + ComponentsList[ IdX ].Height;
For IdX := Low( ComponentsList ) To High( ComponentsList ) Do
ComponentsList[ IdX ].Top := MaxY - ComponentsList[ IdX ].Height;
End;
End;
Just use like this:
CenterControls( [ Button1, Button2, Button4 ] );