相当于代码中的设计指南
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: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 :-)):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这些指南是在设计时代码中实现的,该许可证禁止您随应用程序一起发布,因此您只能使用它来学习,然后自己重新实现。查找
类(在“{RADStudio\version}\source\ToolsAPI 目录”中)。也许可以归结为一些简单的事情,例如
GetMagicConstant
与TControlGuidelines.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
classes (in "{RADStudio\version}\source\ToolsAPI directory"). Perhaps it comes down to something simple as
where
GetMagicConstant
is similar toTControlGuidelines.GetTextBaseline()
.我认为这个逻辑不会以任何方式公开供您在运行时调用。我相信这只是设计时间。
为了解决这个问题,我将在设计器中创建一个虚拟表单,其中包含您使用的每个控件之一。按照屏幕截图中的方式将它们对齐。在运行时实例化此窗体,但不显示它并读出每种控件类型的
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 theTop
property from each type of control to each other type of control.我想将标签与其编辑框对齐。站在@ain的肩膀上,我用了这个:
i wanted to align a label to it's edit box. standing on @ain's shoulders, i used this:
如果您使两个控件具有相同的高度,通常对齐顶部并垂直对齐文本,即使您更改字体大小和字体,基线也会对齐
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