unit AlignChildren.Vcl;
interface
uses
System.Types,
Vcl.Controls;
type
WinControlHelper =
class helper
for TWinControl
public
procedure AlignChildren(ARecursive: Boolean = false);
end;
implementation
{ WinControlHelper }
procedure WinControlHelper.AlignChildren(ARecursive: Boolean);
var
remainingArea: TRect;
childControlIndex: Integer;
currentControl: TControl;
newTop, newLeft: Integer;
reduce: Integer;
begin
remainingArea := Self.ClientRect;
remainingArea.Inflate(-Padding.Left, -Padding.Top, -Padding.Right, -Padding.Bottom);
for childControlIndex := 0
to Self.ControlCount - 1
do
begin
currentControl := Self.Controls[childControlIndex];
if not currentControl.Visible
then
Continue;
newLeft := currentControl.Left;
newTop := currentControl.Top;
case currentControl.Align
of
alTop:
begin
newTop := remainingArea.Top;
if currentControl.AlignWithMargins
then
Inc(newTop, -currentControl.Margins.Top);
reduce := currentControl.Height;
if currentControl.AlignWithMargins
then
Inc(reduce, currentControl.Margins.Top + currentControl.Margins.Bottom);
remainingArea.Inflate(0, -reduce, 0, 0);
end;
alBottom:
begin
newTop := remainingArea.Bottom - currentControl.Height;
if currentControl.AlignWithMargins
then
Inc(newTop, -currentControl.Margins.Bottom);
reduce := currentControl.Height;
if currentControl.AlignWithMargins
then
Inc(reduce, currentControl.Margins.Top + currentControl.Margins.Bottom);
remainingArea.Inflate(0, 0, 0, -reduce);
end;
alLeft:
begin
newLeft := remainingArea.Left;
if currentControl.AlignWithMargins
then
Inc(newLeft, -currentControl.Margins.Left);
reduce := currentControl.Width;
if currentControl.AlignWithMargins
then
Inc(reduce, currentControl.Margins.Left + currentControl.Margins.Right);
remainingArea.Inflate(-reduce, 0, 0, 0);
end;
alRight:
begin
newLeft := remainingArea.Right - currentControl.Width;
if currentControl.AlignWithMargins
then
Inc(newLeft, -currentControl.Margins.Right);
reduce := currentControl.Width;
if currentControl.AlignWithMargins
then
Inc(reduce, currentControl.Margins.Left + currentControl.Margins.Right);
remainingArea.Inflate(0, 0, -reduce, 0);
end;
end;
currentControl.Left := newLeft;
currentControl.Top := newTop;
if ARecursive
and (currentControl
is TWinControl)
then
TWinControl(currentControl).AlignChildren(ARecursive);
end;
end;
end.