本文介绍了如何有效地让一个“ParentFont = False”子控件使用与父母相同的字体名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数VCL控件具有字体 ParentFont 属性。设置 ParentFont = True 是一个很好的做法,字体将跟随它的父字体的名称高度颜色。这样可以在控件之间提供统一的视觉效果。

Most VCL controls has Fonts and ParentFont property. It is a good practice to set ParentFont = True and the Fonts will follow it's parent font's Name, Height and Color. This give a uniform visual among controls.

但是,我们可能希望通过设置 Font来使一个或两个控件与其他控件不同。 Style = fsBold 或对比 Font.Color ,但使用相同的 Font.Name 作为父级字体这样做会使 ParentFont = false 。从此以后,更改父母的字体名称或大小将对这些控件的字体属性没有影响。

However, we may want to make one or two controls to have different looks from other by setting Font.Style = fsBold or a contrast Font.Color but using same Font.Name as parent's font. Doing so makes ParentFont = false. From this point onward, changing parent's font name or size will have no effects on those control's font property.

我认为这可能是VCL的设计。也许有人有更好的设计实践或经验来分享Fonts和ParentFont问题。

I think this might be VCL's design. Perhaps someone has better design practices or experience to share on Fonts and ParentFont issue.

考虑一个例子,我让用户为应用程序设置一个默认的字体名称。那些 ParentFont = False 控件不会相应改变。编码中的手动覆盖是可行的,但是引入额外的编码是繁琐的工作。

Consider a case where I let user set a default font name for the application. Those ParentFont = False controls will not change accordingly. Manual override in coding is possible but it is tedious work that introduce extra coding.

推荐答案

这是已知的VCL限制。

That is known VCL limitation.

您可以拥有 ParentFont 或您的自定义字体设置,在这种情况下,更改父级中的字体属性将不会被传播

You can either have ParentFont or your custom font settings in which case changing font properties in parent will not be propagated.

最好的办法是使用 ParentFont = true ,并设置特定控件的自定义字体属性运行时在窗体的 OnCreate 事件。当然,在这种情况下,您将在设计时失去您所看到的是您所获得的,但您可以更好地控制表单的实际运行时外观。

The best way around that is to use ParentFont = true everywhere and set custom font properties of specific controls at runtime in form's OnCreate event. Of course, in that case you lose What You See Is What You Get at design time, but you get more control over actual runtime appearance of your forms.

procedure TForm1.OnCreate(Sender: TObject);
begin
  inherited;
  Label1.Font.Style := [fsBold];
  Label1.Font.Color := clRed;
end;

为了应用用户自定义字体选择,您还需要重新创建表单,或使用 ParentFont:= true 在应用特定控件的自定义样式之前,以便他们选择您的新字体设置。

For applying user custom font selection, you would also need to recreate forms, or use ParentFont := true before applying custom styles for specific controls, so they pick up your new font settings.

这篇关于如何有效地让一个“ParentFont = False”子控件使用与父母相同的字体名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 22:05