Delphi中如何实现元素居中显示?

更新于
2026-10-05 02:01:21
0阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计280个文字,预计阅读时间需要2分钟。

伪原创改写如下:

类似对齐属性颇具效果,但能对齐元素,因为所有元素都小于容器大小,所以面板上的所有元素都将对齐到底部元素?就像顶部中心的左右分布。这样的分布:或者至少水平平衡。

似乎对齐属性非常有效,但是可以对齐元素,因此如果所有元素都小于容器大小,那么面板上的所有元素都将对齐到彼此底部的所有元素?像顶部中心的东西.

像这样的东西:

或至少水平,垂直,他们可以100%.

将元素放入自己的容器中,例如TPanel或TFrame,它是主容器的子容器.将子容器的Align属性设置为alCustom,并使用父容器的OnAlignPosition事件使子容器保持居中:

// 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;

本文共计280个文字,预计阅读时间需要2分钟。

伪原创改写如下:

类似对齐属性颇具效果,但能对齐元素,因为所有元素都小于容器大小,所以面板上的所有元素都将对齐到底部元素?就像顶部中心的左右分布。这样的分布:或者至少水平平衡。

似乎对齐属性非常有效,但是可以对齐元素,因此如果所有元素都小于容器大小,那么面板上的所有元素都将对齐到彼此底部的所有元素?像顶部中心的东西.

像这样的东西:

或至少水平,垂直,他们可以100%.

将元素放入自己的容器中,例如TPanel或TFrame,它是主容器的子容器.将子容器的Align属性设置为alCustom,并使用父容器的OnAlignPosition事件使子容器保持居中:

// 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;