Delphi element alignment – center

It seems that the alignment attribute is very effective, but you can align elements, so if all elements are smaller than the container size, all elements on the panel will be aligned to all elements at the bottom of each other? Something like the top center.

Something like this:

Or at least horizontally, vertically, they can be 100%.

Put the element in its own container, such as TPanel or TFrame, which is a sub-container of the main container. Set the Align property of the sub-container to alCustom And use the OnAlignPosition event of the parent container to keep the child container in the center:

// Panel1 is the Parent container for the child panel...
procedure TMyForm.Panel1AlignPosition(Sender: TWinControl; Control: TControl;
var NewLeft, NewTop, NewWidth, NewHeight: Integer; var AlignRect: TRect;
AlignInfo: TAlignInfo);
begin
if Control = ChildPanel then
begin
NewLeft := AlignRect.Left + ((AlignRect.Width-Control.Width) div 2);
NewTop := AlignRect.Top + ((AlignRect. Height-Control.Height) div 2);
end;
end;

It seems that the alignment attribute is very effective, but it can align elements, so if All the elements are smaller than the container size, so all the elements on the panel will be aligned to all the elements at the bottom of each other? Something like the top center.

Something like this:

Or at least horizontally, vertically, they can be 100%.

Put the element into its own container, such as TPanel or TFrame, which is a child container of the main container. Set the Align property of the child container to alCustom, and use the OnAlignPosition event of the parent container to keep the child container Centering:

// Panel1 is the Parent container for the child panel...
procedure TMyForm.Panel1AlignPosition(Sender: TWinControl; Control: TControl;
var NewLeft, NewTop, NewWidth, NewHeight: Integer; var AlignRect: TRect;
AlignInfo: TAlignInfo);
begin
if Control = ChildPanel then
begin
NewLeft := AlignRect.Left + ((AlignRect.Width-Control.Width) div 2);
NewTop := AlignRect.Top + ((AlignRect.Height-Control.Height) div 2);
end;
end;

Leave a Comment

Your email address will not be published.