如何在 Lazarus 中指定 child padding/spacing 到 TCustomControl?
How to specify child padding/spacing to a TCustomControl in Lazarus?
在 Delphi 中,我可以使用 Padding
指定任何 child 控件和我的自定义控件之间的间距。这很有用,例如,如果自定义控件在顶部有一个标题部分,因此任何对齐的 child 控件将出现在标题部分下方,它基本上确保 child 控件不能在部分中定位或重叠您不希望他们出现在您的控制之下。
我正试图在 Lazarus 中实现这一点,但由于没有 Padding
属性 我需要一些其他选择。我找到的最接近的东西是 ChildSizing
但我看不到明显的实现方法。
查看这张附加图片:
我的自定义控件 TMemo
是 child 并且实现间距的方式如下:
constructor TMyControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Self.ControlStyle := Self.ControlStyle + [csAcceptsControls];
Self.BorderStyle := bsSingle;
Self.Height := 210;
Self.Width := 320;
Self.ChildSizing.TopBottomSpacing := 10; // This line adds the spacing
end;
正如您从所附图片中看到的那样,控件的顶部和底部有间距,我希望在 属性 名称 TopBottomSpacing
的情况下发生这种情况。
似乎没有明显的 属性 可以处理简单地在顶部、底部、左侧或右侧独立添加间距:
如何在 Lazarus 中单独指定 child 间距?
我只能看到 TopBottomSpacing
和 LeftRightSpacing
有在对边添加间距的不良影响,我只想在顶部添加 padding/spacing。
要指定可以锚定子控件的区域,重写 AdjustClientRect
方法,例如:
procedure TMyControl.AdjustClientRect(var ARect: TRect);
begin
inherited AdjustClientRect(ARect);
ARect := Rect(
ARect.Left,
ARect.Top + Canvas.TextHeight('A'), // Make place for control's header
ARect.Right,
ARect.Bottom);
// Or just:
// ARect.Top += Canvas.TextHeight('A');
end;
另请牢记开发人员的警告:
procedure TWinControl.AdjustClientRect(var ARect: TRect);
begin
// Can be overriden.
// It's called often, so don't put expensive code here, or cache the result
end;
在 Delphi 中,我可以使用 Padding
指定任何 child 控件和我的自定义控件之间的间距。这很有用,例如,如果自定义控件在顶部有一个标题部分,因此任何对齐的 child 控件将出现在标题部分下方,它基本上确保 child 控件不能在部分中定位或重叠您不希望他们出现在您的控制之下。
我正试图在 Lazarus 中实现这一点,但由于没有 Padding
属性 我需要一些其他选择。我找到的最接近的东西是 ChildSizing
但我看不到明显的实现方法。
查看这张附加图片:
我的自定义控件 TMemo
是 child 并且实现间距的方式如下:
constructor TMyControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Self.ControlStyle := Self.ControlStyle + [csAcceptsControls];
Self.BorderStyle := bsSingle;
Self.Height := 210;
Self.Width := 320;
Self.ChildSizing.TopBottomSpacing := 10; // This line adds the spacing
end;
正如您从所附图片中看到的那样,控件的顶部和底部有间距,我希望在 属性 名称 TopBottomSpacing
的情况下发生这种情况。
似乎没有明显的 属性 可以处理简单地在顶部、底部、左侧或右侧独立添加间距:
如何在 Lazarus 中单独指定 child 间距?
我只能看到 TopBottomSpacing
和 LeftRightSpacing
有在对边添加间距的不良影响,我只想在顶部添加 padding/spacing。
要指定可以锚定子控件的区域,重写 AdjustClientRect
方法,例如:
procedure TMyControl.AdjustClientRect(var ARect: TRect);
begin
inherited AdjustClientRect(ARect);
ARect := Rect(
ARect.Left,
ARect.Top + Canvas.TextHeight('A'), // Make place for control's header
ARect.Right,
ARect.Bottom);
// Or just:
// ARect.Top += Canvas.TextHeight('A');
end;
另请牢记开发人员的警告:
procedure TWinControl.AdjustClientRect(var ARect: TRect);
begin
// Can be overriden.
// It's called often, so don't put expensive code here, or cache the result
end;