Default Fonts for Application and Screen Global Objects

Delphi applications start with a predefined default font, determined at design time. This is problematic when you might want the same executable to run smoothly on different versions of Windows, like XP and Vista, that use different default fonts.

The Application global object in Delphi 2009 can provide a standard font to all forms that have ParentFont set to True. To accomplish this you can set the DefaultFont property of the Application. This can be used, for example, to migrate the look and feel of an application to Vista, by setting the font of each form to Segoe UI more easily than in the past. Notice that this property as well as the status of the ParentFont setting is checked when the form is created, and produces no effect if you change it at runtime.

That's why in the AppFont example I've added the font definition code in the project source code file (the CheckWin32Version function is covered later in the section "Extended Vista Support"): begi n

Application.Initialize;

if CheckWin32Version(6) then // at least Vista begi n

Application.DefaultFont.Name := 'Segoe UI'; Application.DefaultFont.Size := 9;

end;

Application.MainFormOnTaskbar := True; Marco Cantu, Delphi 2009 Handbook

At this point all I had to do was to turn on the ParentFont property for each of the two forms at design time. Two buttons, one in each form, display their font name, as a proof that the technique works (although you can probably see the different fonts in those forms). These are the properties of the secondary form:

object FormSecondary: TFormSecondary Caption = 'Secondary' ParentFont = True Visible = True

Now that the forms have the ParentFont property set to True, we can go ahead and dynamically change the font in all forms by writing code like:

procedure TFormAppFont.btnChangeFontClick(

Sender: TObject); begi n

Application.DefaultFont.Name := 'Times New Roman'; Application.DefaultFont.Size := 10; end;

Similarly, the Screen global object can now be used to customize the font used by default in Delphi native message boxes, thanks to the new MessageFont property. After setting this value, you can call routines like ShowMessage and MessageDlg and get a form that uses the given font. This is the case, however, only if the calls are not redirected to use the new task dialogs provided by Vista. In other words, this MessageFont property only has effect if you are not running on Vista or if you are running on Vista and disable the global variable UseLatestCommonDialogs.

Both cases are demonstrated by the event handler of the following button, part of the AppFont application:

procedure TFormAppFont.btnScreenFontClick(

Sender: TObject); begi n

  1. MessageFont.Name := 'Segoe UI'; Screen.MessageFont.Size := Screen.MessageFont.Size + 2; UseLatestCommonDialogs :=
  2. Checked; ShowMessage ( 'Hello') ;

MessageDlg ('Hello', mtINformation, [mbOK], 0); end;

If you are running this program on Vista, by changing the status of the check box, you can see a custom message box with an increasingly larger font or a standard task dialog, with the default system font. Here is an example of this large font in a dialog:

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Responses

  • thomas
    How to change message box font in delphi?
    9 years ago
  • declan
    What fonts to use on form, delpi?
    1 year ago

Post a comment