Font consistency throughout Project?

user1175743 picture user1175743 · May 14, 2012 · Viewed 7.5k times · Source

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?

Answer

David Heffernan picture David Heffernan · May 14, 2012

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.