在表单编辑器中调整其字体大小后调整Delphi表单大小(Resizing Delphi form after resizing its font in form editor)

编程入门 行业动态 更新时间:2024-10-24 14:26:48
表单编辑器中调整其字体大小后调整Delphi表单大小(Resizing Delphi form after resizing its font in form editor)

我开发了一个传统的Delphi 6应用程序,我想增加其中一个表单的字体大小。 所有其他形式都有Microsoft Sans Serif 8pt ,但这一个设置为Microsoft Sans Serif 7pt 。 所有控件都有ParentFont = True ,所以我可以简单地将表单的字体大小设置为8pt 。 问题是表单及其控件不会调整大小,标签文本会重叠。 是否有一种简单的方法可以在调整字体大小后调整窗体大小,而无需在窗体编辑器中手动调整其控件大小?

I develop a legacy Delphi 6 application, and I would like to increase the font size of one of the forms. All other forms have Microsoft Sans Serif 8pt, but this one is set to Microsoft Sans Serif 7pt. All controls have ParentFont = True, so I could simply set the font size of the form to 8pt. The problem is that the form and its controls won't resize, and the text of the labels would overlap. Is there a simple way to resize the form after resizing its font, without resizing its controls manually in the form editor?

最满意答案

在设计时,您可以通过手动编辑.dfm文件来实现更改。 确保在Scaled属性设置为True情况下保存表单。

然后,在Delphi中关闭您的项目,或者完全关闭Delphi。 接下来,在文本编辑器中打开.dfm文件,然后调整表单TextHeight属性。 例如,如果要从7pt缩放到8pt,并且TextHeight设置为13 ,则应将其更改为11 。 然后重新加载项目并在设计器中打开表单,您的表单将被缩放。 这不是一个完美的缩放,因为TextHeight不允许浮点值。 但它可能已经足够好了。


在运行时,您可以调用ChangeScale :

ChangeScale(NewFont.Size, OldFont.Size);

请注意, ChangeScale是受保护的成员。 因此,根据您调用此处的位置,您可能需要使用受保护的成员hack。

然后,一种选择是在运行时调用表单持久性框架以生成.dfm文件的缩放版本。 这样可以让你比使用TextHeight玩技巧更精确

例如,您可以将以下内容附加到表单的OnShow事件:

procedure TMyForm.FormShow(Sender: TObject); var BinaryStream, TextStream: TStream; begin BinaryStream := TMemoryStream.Create; Try BinaryStream.WriteComponent(Self); BinaryStream.Position := 0; TextStream := TFileStream.Create('MyForm.dfm', fmCreate); Try ObjectBinaryToText(BinaryStream, TextStream); Finally TextStream.Free; End; Finally BinaryStream.Free; End; end;

这将基于运行时状态生成新的.dfm文件。 然后,您可以将其与版本控制系统中的.dfm文件的版本进行比较。 您将不会接受一些更改,但主要是更改将是您想要的位置和大小更改。

At designtime you can effect the change by editing the .dfm file manually. Make sure that the form is saved with the Scaled property set to True.

Then, close your project in Delphi, or close Delphi altogether. Next open the .dfm file in a text editor and adjust the forms TextHeight property. For example, if you want to scale from 7pt to 8pt, and TextHeight is set to 13, then you should change it to 11. Then re-load the project and open the form in the designer and your form will be scaled. This won't be a perfect scaling because you aren't allowed floating point values for TextHeight. But it may be good enough.


At runtime, you can to call ChangeScale:

ChangeScale(NewFont.Size, OldFont.Size);

Note that ChangeScale is a protected member. So, depending on where you are calling this, you may need to use the protected member hack.

One option then would be to call the form persistence framework at runtime to generate a scaled version of the .dfm file. That would allow you more precise control than playing tricks with TextHeight

For example, you can attach the following to the OnShow event of your form:

procedure TMyForm.FormShow(Sender: TObject); var BinaryStream, TextStream: TStream; begin BinaryStream := TMemoryStream.Create; Try BinaryStream.WriteComponent(Self); BinaryStream.Position := 0; TextStream := TFileStream.Create('MyForm.dfm', fmCreate); Try ObjectBinaryToText(BinaryStream, TextStream); Finally TextStream.Free; End; Finally BinaryStream.Free; End; end;

This will generate a new .dfm file based on the runtime state. You can then compare this with the version of the .dfm file that is in your revision control system. There will be a few changes that you won't want to accept, but mostly the changes will be the position and size changes that you do want.

更多推荐

本文发布于:2023-07-16 15:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1130529.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   字体大小   器中   大小   编辑

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!