如何有效地让一个“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. 本地模拟服务器CDN(静态HTML,CSS,JS)开发

    本地模拟服务器CDN(静态HTML,CSS,JS)开发 所谓本地开发环境就是和线上cdn(a.longencdn.cn)一样的目录结构和功能,提供了一个本地镜像,开发者直接在本地镜像的对应目录中作开发 ...

  2. linux内核中socket的创建过程源码分析(详细分析)

    1三个相关数据结构. 关于socket的创建,首先需要分析socket这个结构体,这是整个的核心. 104 struct socket { 105         socket_state       ...

  3. Python2.7-weakref

    weakref 模块,允许创建对象的弱引用,被弱引用的对象其引用计数不变,对象的引用计数为0时就会被垃圾清理机制释放内存空间,此时对其的弱引用也会失效.在对象会被交叉引用,需要释放内存空间时常用. 模 ...

  4. Ajax的用法

    1 Ajax是什么 1.1 Asynchronous JavaScript and XML(异步的javascript和xml) 实质为:使用浏览器内置的一个对象(XmlHttpRequest)向服务 ...

  5. form表单,submit,ajax提交

    尼玛... 一个简单的表单提交,竟然给我整的直郁闷. 本来就是个保存功能,几个前人都用的ajax提交,我也就没改成submit.然后坑爹的就来了. 我在表单里写了个<form></f ...

  6. odoo方法

    一. 类似于前面有个_ 的方法,格式是如下def _getdlvqty(self, cr, uid, ids, field_names, args, context=None): def _get_p ...

  7. 20155227《网络对抗》Exp3 免杀原理与实践

    20155227<网络对抗>Exp3 免杀原理与实践 实践内容 正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,自己利用shellcode编程等 ...

  8. 20155232《网络对抗》Exp4 恶意代码分析

    20155232<网络对抗>Exp4 恶意代码分析 1.实践目标 1.1是监控你自己系统的运行状态,看有没有可疑的程序在运行. 1.2是分析一个恶意软件,就分析Exp2或Exp3中生成后门 ...

  9. 20155320 Exp7 网络欺诈防范

    20155320 Exp7 网络欺诈防范 [基础问题回答] (1)通常在什么场景下容易受到DNS spoof攻击 乱点链接或者连公共场合的免费WiFi也容易受到攻击,尤其是那种不需要输入密码直接就可以 ...

  10. 2017-2018-2 《网络对抗技术》20155322 Exp9 web安全基础

    [-= 博客目录 =-] 1-实践目标 1.1-实践介绍 1.2-实践内容 1.3-实践要求 2-实践过程 2.1-HTML 2.2-Injection Flaws 2.3-XSS 2.4-CSRF ...