Fonts

Introduction

A font is a series of characteristics and properties that control how text displays on a device such as a monitor, a printer, or paper, etc. The Visual Component Library (VCL) provides a class used to create, modify, or define font characteristics.

The TFont Class

Most of the classes used to display text of any kind are equipped with a TFont member variable. Based on this, you usually don't need to declare a TFont instance to control how text for a control displays. All you have to do is to call the Font member variable and modify its characteristics. The properties of the TFont class are defined in the help files. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Label1->Font->Name = "Times New Roman";
    Label1->Font->Size = 36;
    Label1->Font->Color = clBlue;
}
//---------------------------------------------------------------------------

As stated already, many controls possess the Font property.

In some situations, you may need to explicitly create a font, define its characteristics, and use it to control the display of a string. To create a font, you can use the TFont class. As a descendent of TObject, the TFont must be created as a pointer, using the new operator. If you simply declare an instance of a TFont class, you don't have to define its characteristics if you can be satisfied with the default properties. A TFont class is by default configured to use the MS Sans Serif with a size of 8 and a color of clWindowText. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
    TFont *FT = new TFont;
    Label1->Font = FT;
    delete FT;
}
//---------------------------------------------------------------------------

After declaring an instance of a TFont class, you can modify its default properties as you wish. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
    TFont *FT = new TFont;

    FT->Name = "Algerian";
    FT->Size = 35;
    FT->Color = clBlue;
    FT->Charset = ANSI_CHARSET;
    FT->Style = TFontStyles() << fsBold;

    Label1->Font = FT;

    delete FT;
}
//---------------------------------------------------------------------------

The Win32 API Font Management

Besides the VCL's TFont class, the Win32 library provides many more possibilities for font management through a series of structures and functions. The most fundamental means of creating a font is by calling the Win32 API's CreateFont() function. This function takes quite a few arguments and returns an HFONT object. You can therefore create a font that returns a handle and assign that handle to the TFont::Handle property. Here is an example: 

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    HFONT hFont = CreateFont(24, 20, (10*45), (10*45),
                             FW_THIN, FALSE, FALSE, FALSE,
                             ANSI_CHARSET, OUT_DEFAULT_PRECIS,
                             CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
                             FF_DONTCARE, "Georgia");

    Canvas->Font->Handle = hFont;
    Canvas->TextOut(100, 325, "Ferdinand Oyono");
}
//---------------------------------------------------------------------------

Because the Create function behaves like a strict C++ function, you must provide a value to each of its arguments even if you want to use only a few. An alternative is to create a font using the LOGFONT structure. The advantage with the LOGFONT structure is that, like any other structure or class, you can provide values only for the variables you want to change; the Win32 API can use default values for the other variable. After "filling out" the structure, you can call the CreateFontIndirect() function. This function takes one argument which is constant pointer to LOGONT. The CreateFontIndirect() function also returns an HFONT. This means that you can assign its return value to the TFont::Handle property. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    LOGFONT lFont;

    lFont.lfHeight = 24;
    lFont.lfWidth = 20;
    lFont.lfEscapement = -10*15;
    lFont.lfOrientation = -10*15;
    lFont.lfWeight = FW_BOLD;
    lFont.lfItalic = TRUE;
    lFont.lfUnderline = False;
    lFont.lfStrikeOut = False;
    lFont.lfCharSet = ANSI_CHARSET;
    lFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
    lFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
    lFont.lfQuality = DEFAULT_QUALITY;
    lFont.lfPitchAndFamily = FF_DONTCARE;
    strcpy(lFont.lfFaceName, "Courier New");

    HFONT hfont = CreateFontIndirect(&lFont);
    Canvas->Font->Handle = hfont;
    Canvas->TextOut(40, 25, "Toussaint Louverture");
}
//---------------------------------------------------------------------------
 

Copyright 2003-2007 FunctionX, Inc.