Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi element alignment - center

There seems to be align property that works really well, but is is possible to align element so all elements on panel would be aligned to center all on bottom of each other if they all have less than size of container? Something like top-center-center.

Something like this:

Something like this

Or at least horizontally, and vertically they can have 100%.

like image 244
Flash Thunder Avatar asked May 19 '14 17:05

Flash Thunder


2 Answers

Put the elements into their own container, such as a TPanel or TFrame, that is a child of your main container. Set the child container's Align property to alCustom and use the parent container's OnAlignPosition event to keep the child container centered to itself:

// 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;
like image 152
Remy Lebeau Avatar answered Sep 28 '22 22:09

Remy Lebeau


There is no need for coding anything. Just place panels and other visual objects in the right way an set the properties of the visual objects as shown here:

Align: alNone or alCustom
and
Anchors: none (akLeft=False, akTop=False, akRight=False, akBottom=False)

Than and an object will stay at its relative horizontal and vertical position. If you place it in the middle in a container it will stay centered.

To center it only vertical set

Align: alNone or alCustom
and
Anchors: akTop=True OR akBottom=True

To center it only horizontal set

Align: alNone or alCustom
and
Anchors: akLeft=True OR akRight=True
like image 28
cmks Avatar answered Sep 28 '22 22:09

cmks