如何有效地让一个“ParentFont = False”子控件使用与父母相同的字体名称?(How to efficiently let a `ParentFont = False` child control to use same font name as parent?)

问 题
 

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.

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.

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

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.

解决方案

That is known VCL limitation.

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

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;

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.

本文地址:IT屋 » How to efficiently let a `ParentFont = False` child control to use same font name as parent?

问 题
 

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

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

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

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

解决方案

这是已知的VCL限制。

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

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

procedure TForm1.OnCreate(Sender:TObject); 
开始
继承;
Label1.Font.Style:= [fsBold];
Label1.Font.Color:= clRed;
结束

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

最新文章

  1. hihoCoder #1445 : 后缀自动机二·重复旋律5
  2. 【Leetcode】Longest Palindromic Substring
  3. python学习笔记4-redis multi watch实现锁库存
  4. Castle.ActiveRecord 多对多关系 引发的错误处理
  5. github生成燃尽图
  6. js追加元素,以及元素位置
  7. C#读取shapefile文件(不用AE)
  8. jdk线程的死锁
  9. How to Build FFmpeg for Android
  10. iOS开发——技术精华Swift篇&Swift 2.0和Objective-C2.0混编之第三方框架的使用
  11. Unity3D开发(五):Unity3D 4.x 使用Mecanim实现连击(转)
  12. 记一次Mysql魔鬼实训
  13. css基础教程
  14. jenkins忘记admin密码解决办法
  15. LayaAir引擎开发HTML5最简单教程(面向JS开发者)
  16. C# 对象持久化
  17. C# json、xml序列化
  18. Wincc用户登录VBS脚本
  19. [hdu P4334] Trouble
  20. Jordan 块的几何

热门文章

  1. golang 文件服务器
  2. 《Head First 设计模式》[01] 策略模式
  3. CM (Cloudera Manager) 的安装,便于CDH的离线部署
  4. 在Docker中安装.NET Core(使用命令行工具)
  5. 20155318 Exp1 PC平台逆向破解(5)M
  6. MiZ702学习笔记10——文本实例化IP的方法
  7. R绘图 第五篇:绘制散点图(ggplot2)
  8. Scrapy持久化存储
  9. 高精度减法--C++
  10. [PLC]ST语言四:INV_MEP_MEF_PLS_PLF_MC_MCR