整个项目的字体一致性? [英] Font consistency throughout Project?

查看:155
本文介绍了整个项目的字体一致性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种快速有效的方式应用在项目中使用的全局字体?



这样我的意思是我会喜欢设置一个特定的字体名称,我的项目中的所有控件都将使用,如TButton,TEdit,TLabel等。



通常为表单设置Font而不是特定的控件会将该表单上的所有控件更改为指定的字体。



但是,如果您在特定控件上手动更改了字体,则会出现一些问题,然后通过表单设置字体将不再更新以前已手动更改的控件。



想法1



我正在考虑使用For循环并遍历我的Forms上的每个组件,并以这种方式设置Font,例如:

  procedure TForm1.FormCreate(Sender:TObject); 
var
i:整数;
begin
with TForm(Self)do
begin
for i:= 0 to ComponentCount - 1 do
begin
如果Components [i]是TButton然后
begin
TButton(Components [i])。Font.Name:='MS Sans Serif';
TButton(Components [i])。Font.Size:= 8;
TButton(Components [i])。Font.Style:= [fsBold];
结束

如果组件[i]是TLabel,然后
begin
TLabel(Components [i])。Font.Name:='MS Sans Serif';
TLabel(Components [i])。Font.Size:= 8;
TLabel(Components [i])。Font.Style:= [fsBold];
结束
结束
结束
结束

但这样做似乎很凌乱,它也将是一个相当数量的代码,一个简单的任务。



想法2



我知道我可以在设计时手动更改字体一个接一个地为每一个控制,但有几种形式来通过这可能需要时间,即使那时我可能会错过一个控制。



想法3



与Idea 2类似,另一种方法可能是以Form(DFM)形式查看表单,并以这种方式查找和替换该字体。






基本上,我将在我的应用程序中保持一致性,并且整个使用一个字体是我想要实现的。



我在这里缺少一些完全明显的东西,是为了这样一个任务,我正在努力做些什么?

解决方案

正如评论中所讨论的,这个关键是 ParentFont 财产。该属性在VCL层次结构中的各个点定义。如果为链中的所有组件设置 ParentFont True ,则可以更改整个应用程序的字体只需修改

  Application.DefaultFont 

默认情况下,大多数组件将 ParentFont 设置为 True ,因此您无关。奇怪的是 TForm 。一个全新的默认表单将 ParentFont 设置为 False 。这有点令人失望,但我怀疑反映了VCL的原始设计师并没有预料到这一点,而且在VCL开发的相对较晚的时候, ParentFont 被嫁接。 / p>

无论在理想的世界中,应用程序中的所有表单都应该从您控制的公共基类派生。如果是这样,那么你可以在那里进行更改,将 ParentFont 设置为 True ,确保没有显式字体设置适用于您的表单上的任何组件,您是金色的。通过单个属性控制整个应用程序的字体。如果您的表单没有共同的基类,这里是添加它的理想时间。如果您不想这样做,则需要为每个表单设置 ParentFont



其他相关属性是 Screen.MessageFont Screen.MenuFont 。这些提供对消息框和菜单中使用的字体的全局控制。然而,Delphi的最新版本已经转移到Windows控件上的消息框和菜单的绘画,因此这些属性没有任何影响。


Is there a quick and effective way of applying a global Font to be used in a project?

By this I mean I would like to set a specific Font name that all controls in my project will use such as TButton, TEdit, TLabel etc.

Typically setting the Font for the Form rather than a specific control will change all the controls on that Form to the Font specified.

There is a slight issue with this however, if you have manually changed a Font on a specific control, then setting the Font by the Form will no longer update those controls that have previously been changed manually.

Idea 1

I was thinking of using a For loop and iterating through each component on my Forms and setting the Font this way, such as:

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  with TForm(Self) do
  begin
    for i := 0 to ComponentCount - 1 do
    begin
      if Components[i] is TButton then
      begin
        TButton(Components[i]).Font.Name  := 'MS Sans Serif';
        TButton(Components[i]).Font.Size  := 8;
        TButton(Components[i]).Font.Style := [fsBold];
      end;

      if Components[i] is TLabel then
      begin
        TLabel(Components[i]).Font.Name  := 'MS Sans Serif';
        TLabel(Components[i]).Font.Size  := 8;
        TLabel(Components[i]).Font.Style := [fsBold];
      end;
    end;
  end;
end;

But doing this seems very messy, it will also be a considerable amount of code for a simple task.

Idea 2

I know I could manually change the fonts at design time one by one for each control, but with several forms to go through this could take time and even then I might of missed a control.

Idea 3

Similar to Idea 2, another way could be to view the Form as Text (DFM) and Find and Replace the font that way.


Basically I am going for consistency within my Application, and having one Font used throughout is what I was looking to achieve.

Am I missing something completely obvious here, is what I am trying to do overkill for such a task?

解决方案

As discussed in the comments, the key to this is the ParentFont property. This property is defined at various points in the VCL hierarchy. If you set ParentFont to be True for all components in the chain, then you can change the fonts for the entire application simply by modifying

Application.DefaultFont

By default most components set ParentFont to True and so you have nothing to do. The odd one out though is TForm. A brand new default form has ParentFont set to False. This is somewhat disappointing but I suspect reflects the fact that the original designers of the VCL did not anticipate this and that ParentFont was grafted on relatively late in the development of the VCL.

No matter, in an ideal world, all forms in your application should be derived from a common base class that you control. If that is so then you can simply make the change there, set ParentFont to be True, make sure no explicit font settings are applied to any components on you forms, and you are golden. Control the entire application's fonts through a single property. If you don't have a common base class for your forms, here's an ideal time to add it. If you don't want to do that then you need to set ParentFont for each form.

Other related properties are Screen.MessageFont and Screen.MenuFont. These provide global control over the fonts used in message boxes and menus. However, recent versions of Delphi have handed back to Windows control over the painting of message boxes and menus and so these properties have no effect.

这篇关于整个项目的字体一致性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆