【问题标题】:Delphi element alignment - centerDelphi 元素对齐 - 居中
【发布时间】:2014-07-07 18:50:48
【问题描述】:

似乎align 属性工作得非常好,但是可以对齐元素,因此如果面板上的所有元素都小于容器大小,那么它们将对齐到彼此底部的中心?类似于 top-center-center。

类似这样的:

或者至少水平和垂直它们可以有 100%。

【问题讨论】:

  • 这可以通过使用中间容器来完成...使用 FireMonkey 和最新版本的 Delphi 进行布局...或另一个没有边框的 TPanel。
  • 怎么样?没有alCenter...
  • 请添加Delphi版本和平台(VCL/FM)
  • @RemyLebeau 没有用。
  • @FlashThunder:当我尝试它时,对我来说效果很好。但是容器必须在开始时位于中心,然后它将保持居中(更准确地说,它将保持相对于其起始位置)。更可靠的解决方案是将容器的Align 设置为alCustom 并使用其父容器的OnAlignPosition 事件来保持子容器居中。请参阅我发布的答案。

标签: delphi vcl delphi-xe6


【解决方案1】:

在 RAD 10+ 中有一个控件 TRelativePanel,它具有 AlignVerticalCenterWithPanel 和 AlignHorisontalCenterWithPanel 救生选项(以及其他有用的功能)。

您还可以在中心放置不可见的线或点,并使用 TRelativePanel 提供的属性 Above/Below/等围绕它构建其他控件。 值得一提的是,控制是根据顶级 Embarcadero 质量标准进行的(仅在设计模式下崩溃)。

【讨论】:

    【解决方案2】:

    无需编写任何代码。只需以正确的方式放置面板和其他视觉对象,并设置视觉对象的属性,如下所示:

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

    Than 和一个对象将保持在其相对水平和垂直位置。如果将它放在容器的中间,它将保持居中。

    仅将其居中设置为垂直设置

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

    仅将其居中设置为水平设置

    Align: alNone or alCustom
    and
    Anchors: akLeft=True OR akRight=True
    

    【讨论】:

      【解决方案3】:

      你可以用这个小程序来控制中心

      procedure CenterControl( AControl : TControl );
      begin
        if Assigned( AControl.Parent )
        then
          begin
            // remove alignment
            AControl.Align := alNone;
            // remove the anchors
            AControl.Anchors := [];
            // center on parent
            AControl.Left := ( AControl.Parent.ClientWidth - AControl.Width ) div 2;
            AControl.Top := ( AControl.Parent.ClientHeight - AControl.Height ) div 2;
          end
        else
          raise Exception.Create( 'Control needs a Parent!' );
      end;
      

      如果调整父级控件的大小,只要您没有更改其大小,控件将始终居中。

      【讨论】:

      • 最好使用VCL的原生对齐功能。如果不出意外,父控件知道其所有子控件,甚至是自定义对齐的子控件,并计算允许它们相对于对齐的区域。
      【解决方案4】:

      将元素放入它们自己的容器中,例如 TPanelTFrame,它们是主容器的子容器。将子容器的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;
      

      【讨论】:

      • 哦thanx,不知道怎么用这个alCustom
      • 另一种方法是去掉子容器,然后在元素本身上设置alCustom,然后父容器的OnAlignPosition可以根据需要直接定位元素。
      • 这种与 alCustom 的中心对齐仅在运行时发生。还有一种方法可以在设计时发生,例如 alClient、alLeft 等等..?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 2014-08-10
      • 2015-04-26
      • 1970-01-01
      • 2019-04-24
      相关资源
      最近更新 更多