如何有效地让一个“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 在应用特定控件的自定义样式之前,以便他们选择您的新字体设置。

如何有效地让一个“ParentFont = False”子控件使用与父母相同的字体名称?的更多相关文章

  1. WPF silverlight获取子控件(获取DataTemplate里的子控件)

    public static class VisualTreeExtensions { /// <summary> /// 获取父节点控件 /// </summary> /// ...

  2. 记录下帮助一位网友解决的关于android子控件的onTouch或onClick和父OnTouch 冲突的问题。

    前三天收到位网友的私信求助,问题大概如标题所示.具体是下面的情况,个人感觉,这个问题挺有趣,也会在实际项目开发中很常见.不想看前奏的请直接跳至解决方法. 问题原型: 父控件是自定义的 LinearLa ...

  3. ParentWindow属性及其一系列函数的作用——适合于那些不需要父控件管理内存释放的子控件

    TWinControl = class(TControl) property ParentWindow: HWnd read FParentWindow write SetParentWindow; ...

  4. OnClick事件的Sender参数的前世今生——TWinControl.WinProc优先捕捉到鼠标消息,然后使用IsControlMouseMsg函数进行消息转发给图形子控件(意外发现OnClick是由WM_LBUTTONUP触发的)

    这是一个再普通不过的Button1Click执行体: procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage('I am B ...

  5. 五种情况下会刷新控件状态(刷新所有子FWinControls的显示)——从DFM读取数据时、新增加子控件时、重新创建当前控件的句柄时、设置父控件时、显示状态被改变时

    五种情况下会刷新控件状态(刷新控件状态才能刷新所有子FWinControls的显示): 在TWinControls.PaintControls中,对所有FWinControls只是重绘了边框,而没有整 ...

  6. 解决ListView中Item的子控件与Item点击事件冲突

    常常会碰到在ListView中点击当中一个Item.会一并触发其子控件的点击事件.比如Item中的Button.ImageButton等.导致了点击Item中Button以外区域也会触发Button点 ...

  7. 截取scrollview的滑动事件,传递给子控件

    重写一个ScrollView public class MyScrollView extends ScrollView{ public MyScrollView(Context context, At ...

  8. TCustomControl绘制自己和图形子控件共四步,TWinControl关键属性方法速记

    TCustomControl = class(TWinControl) private FCanvas: TCanvas; procedure WMPaint(var Message: TWMPain ...

  9. 初步探讨WPF的ListView控件(涉及模板、查找子控件) - GavinJun

    本文结合模板的应用初步介绍ListView的应用 一.Xaml中如何建立数据资源 大部分数据都会来自于后台代码,如何Xaml同样的建立数据源呢?比如建立一个学生List: 首先引入命名空间: xmln ...

随机推荐

  1. CentOS配置Hive

    hive搭建共分为三种模式:1.embedded,2.local,3.remote server 在这里,主要是配置第3种模式:remote server模式,如下图所示: 我的环境共三台虚拟机:Ho ...

  2. day34

    今日内容: 1.开启子进程的两种方式 2.join方法 3.进程之间内存空间相互隔离 4.僵尸进程和孤儿进程 5.守护进程 6.互斥锁 7.进程对象其他相关的属性和方法 1.开启子进程的两种方式 方式 ...

  3. day05今日学习总结:字符串类型

    昨日学习复习: 数据类型: 有序.无序 有序:可以根据索引查找的数据 可变不可变 可变:在值变的情况下,id不变,证明原值是在改变的 不可变:在值变的情况下,id也跟着变,证明不是在改原值. 今日学习 ...

  4. Oracle substr() instr() 用法

    转载:oracle中substr() instr() 用法 substr(字符串,截取开始位置,截取长度) = 返回截取的字符串instr(源字符串,目标字符串,起始字符串,匹配字符串) = 返回要截 ...

  5. python 字典,元组,对象,数组取值方法

    def create(self,cr,uid,vals,context=None): if context is None: context ={} if vals.get('name','/')== ...

  6. Android开发四大组件之Service(具体解释篇)

    Android开发之四大组件--Service 一.Service 简单介绍 Service是android系统中的四大组件之中的一个(Activity.Service.BroadcastReceiv ...

  7. 带你看懂大数据采集引擎之Flume&采集目录中的日志

    一.Flume的介绍: Flume由Cloudera公司开发,是一种提供高可用.高可靠.分布式海量日志采集.聚合和传输的系统,Flume支持在日志系统中定制各类数据发送方,用于采集数据:同时,flum ...

  8. WPF之Manipulation

    原文:WPF之Manipulation 需求:现,在窗口下有一个StackPanel控件. 1.可以拖动. 2.可以展开及收缩(不仅仅可以拖动还可以点击) 3.窗口向坐标轴一样分四个象限,在不同的区域 ...

  9. STM32-M0中断优先级介绍

    先解释中断优先级,后面讲代码的实现. 差异:M0的中断优先级相比于M4,没有用到分组,且只用到了2个bit位(即0~3)来设置,数值越小,优先级越高:同等优先级,根据终端号的大小来决定谁先执行. 根据 ...

  10. 写个发邮件的功能php的(全代码)

    ---恢复内容开始--- 正好做了个项目,需要在线留言,一般在线留言发邮件是很常见的方式,一开始从网上搜了很久都没有很全的,也有全一点的,但是也不能用,运行不成功,下面给大家分享一下运行成功了的全部代 ...