相当于代码中的设计指南

发布于 2024-11-28 04:02:22 字数 1065 浏览 1 评论 0原文

VCL 表单设计器提供了粉色指南,用于在各自的文本基线上对齐控件: 表单设计器指南
但据我所知,这不适用于标签和复选框。 更新:如果您准确地放置控件,它就适用于标签,例如通过Ctrl-箭头。它适用于复选框 - 请参阅屏幕截图。

现在,在某些表单上,我正在代码中创建控件,例如

ed := TEdit.Create(Self);
ed.SetBounds(...);
ed.Parent := SomePanel;

等。如何确保它们的文本基线对齐?我想用它来编辑、组合框、标签和复选框。结果应该如下所示(当然没有红线:-)): 基线对齐

编辑: 我当前的方法是调用类似 AlignTop(8 , [Edit1, ComboBox1], [CheckBox1, Label1]);

procedure ControlArray_SetTop(const AControls: array of TControl; ATop: Integer);
var
  i: Integer;
begin
  for i := Low(AControls) to High(AControls) do
    AControls[i].Top := ATop;
end;

procedure AlignTop(ATop: Integer; const AControls: array of TControl; const ALabelLikeControls: array of TControl);
begin
  ControlArray_SetTop(AControls, ATop);
  ControlArray_SetTop(ALabelLikeControls, ATop + 3);
end;

我的目标是用更强大、更少 hack 的东西替换它。

The VCL form designer offers pink guidelines for aligning controls at their respective text base lines:
Guidelines in form designer
But as far as I can tell this doesn't work for labels and checkboxes. Update: It works for labels if you place the controls exactly, e.g. by Ctrl-arrow. It kind of works for checkboxes - see screenshot.

Now, on some forms I'm creating controls in code, e.g.

ed := TEdit.Create(Self);
ed.SetBounds(...);
ed.Parent := SomePanel;

etc. How can I ensure that their text base lines are aligned? I'd like to have this for edits, comboboxes, labels and checkboxes. The result should look like this (without the red line, of course :-)):
base line aligned

Edit: My current approach is to call something like AlignTop(8, [Edit1, ComboBox1], [CheckBox1, Label1]); with

procedure ControlArray_SetTop(const AControls: array of TControl; ATop: Integer);
var
  i: Integer;
begin
  for i := Low(AControls) to High(AControls) do
    AControls[i].Top := ATop;
end;

procedure AlignTop(ATop: Integer; const AControls: array of TControl; const ALabelLikeControls: array of TControl);
begin
  ControlArray_SetTop(AControls, ATop);
  ControlArray_SetTop(ALabelLikeControls, ATop + 3);
end;

My goal is to replace it with something more robust and less hacky.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

昨迟人 2024-12-05 04:02:22

这些指南是在设计时代码中实现的,该许可证禁止您随应用程序一起发布,因此您只能使用它来学习,然后自己重​​新实现。查找

DesignIntf.TBaseComponentGuidelines
DesignEditors.TComponentGuidelines
VCLEditors.TControlGuidelines

类(在“{RADStudio\version}\source\ToolsAPI 目录”中)。也许可以归结为一些简单的事情,例如

Label1.Top := (Edit1.Top + Edit1.Height) - Label1.Height + GetMagicConstant;  

GetMagicConstantTControlGuidelines.GetTextBaseline() 类似。

The guidelines are implemented in designtime code which license prohibits you to ship with your app so you can only use it to learn from it and then reimplement it yourself. Look up

DesignIntf.TBaseComponentGuidelines
DesignEditors.TComponentGuidelines
VCLEditors.TControlGuidelines

classes (in "{RADStudio\version}\source\ToolsAPI directory"). Perhaps it comes down to something simple as

Label1.Top := (Edit1.Top + Edit1.Height) - Label1.Height + GetMagicConstant;  

where GetMagicConstant is similar to TControlGuidelines.GetTextBaseline().

没有你我更好 2024-12-05 04:02:22

我认为这个逻辑不会以任何方式公开供您在运行时调用。我相信这只是设计时间。

为了解决这个问题,我将在设计器中创建一个虚拟表单,其中包含您使用的每个控件之一。按照屏幕截图中的方式将它们对齐。在运行时实例化此窗体,但不显示它并读出每种控件类型的 Top 属性。最后,您可以计算出 Top 属性从每种类型的控件到每种其他类型的控件的垂直偏移量。

I don't think this logic is exposed in any way for you to call at runtime. I believe it is design time only.

To handle this I would create a dummy form in the designer which had one of each control you worked with. Align them all the way you have in your screenshots. At runtime instantiate this form, but don't show it and read out the Top property for each type of control. Finally you can work out the vertical offset of the Top property from each type of control to each other type of control.

何必那么矫情 2024-12-05 04:02:22

我想将标签与其编辑框对齐。站在@ain的肩膀上,我用了这个:

  Label1.Top := edit1.Top + _GetTextBaseline(edit1, tlBottom) - _GetTextBaseline(Label1, tlTop);


  // lifted from TControlGuidelines.GetTextBaseline(AControl: TControl; Align: TTextLayout): Integer;
  function _GetTextBaseline(AControl: TControl; Align: TTextLayout): Integer;
  var
    Canvas: TControlCanvas;
    tm: TTextMetric;
    ClientRect: TRect;
    Ascent, Height: Integer;
  begin
    Canvas := TControlCanvas.Create;
    try
      ClientRect := AControl.ClientRect;
      Canvas.Control := AControl;
      Canvas.Font := TControlFriend(AControl).Font;
      GetTextMetrics(Canvas.Handle, tm);
      Ascent := tm.tmAscent + 1;
      Height := tm.tmHeight;
      case Align of
        tlTop: Result := ClientRect.Top + Ascent;
        tlCenter: Result := (ClientRect.Top + (ClientRect.Bottom - Height) div 2) + Ascent;
        tlBottom: Result := (ClientRect.Bottom - Height) + Ascent;
      else
        Result := 0;
      end;
    finally
      Canvas.Free;
    end;
  end;

i wanted to align a label to it's edit box. standing on @ain's shoulders, i used this:

  Label1.Top := edit1.Top + _GetTextBaseline(edit1, tlBottom) - _GetTextBaseline(Label1, tlTop);


  // lifted from TControlGuidelines.GetTextBaseline(AControl: TControl; Align: TTextLayout): Integer;
  function _GetTextBaseline(AControl: TControl; Align: TTextLayout): Integer;
  var
    Canvas: TControlCanvas;
    tm: TTextMetric;
    ClientRect: TRect;
    Ascent, Height: Integer;
  begin
    Canvas := TControlCanvas.Create;
    try
      ClientRect := AControl.ClientRect;
      Canvas.Control := AControl;
      Canvas.Font := TControlFriend(AControl).Font;
      GetTextMetrics(Canvas.Handle, tm);
      Ascent := tm.tmAscent + 1;
      Height := tm.tmHeight;
      case Align of
        tlTop: Result := ClientRect.Top + Ascent;
        tlCenter: Result := (ClientRect.Top + (ClientRect.Bottom - Height) div 2) + Ascent;
        tlBottom: Result := (ClientRect.Bottom - Height) + Ascent;
      else
        Result := 0;
      end;
    finally
      Canvas.Free;
    end;
  end;
于我来说 2024-12-05 04:02:22

如果您使两个控件具有相同的高度,通常对齐顶部并垂直对齐文本,即使您更改字体大小和字体,基线也会对齐

If you make the two controls of the same height, align the tops and align the text vertically normally the base line will be aligned even when you change font size and fonts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文