Home Articles Books Downloads FAQs Tips

Q: Change a font's face and size from code


Answer

You can change a font's face by assigning a new value to it's Name property. You can change the size of the font by assigning a value to either the font's Size property, or its Height property. The following code changes the font of a label control.

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        Label1->Font->Size = 14;
        Label1->Font->Name = "Courier New";
    }

Note: Before you change the Name property of a font, it is wise to make sure that the font is installed on the system. The global Screen object and the object returned by the global Printers function both contain a Fonts property that lists the fonts installed on the system. The next code example demonstrates how you can determine if a font exists before making a change to the Name property.

    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
        if(Screen->Fonts->IndexOf("Courier New") >= 0)
            Label1->Font->Name = "Courier New";
        else
            ShowMessage("Courier New not found");
    }

Note: The Size property of TFont specifies the height of the font in points. There are approximately 72 points per inch. This means that if you set the Size property of a font to 72, the characters will be 1 inch tall. If you specify a size of 12, the characters will be 1/6 of an inch tall.

Note: The Height property of TFont specifies the height of the font in pixels. When you use the small fonts control panel setting, there are 96 pixels per inch. Since there are 72 points per inch, and 96 pixels per inch, if you set the Size property to 72, then the Height of the font will be 96 pixels. You can test this theory by setting the Size of font to 72 using the object inspector. The Height of a font can be calculated from its size by the following equation

    Height = - Size * PixelsPerInch / 72

Note: When the Size property is a positive value, the Height property will be negative. When Size is negative, Height is positve. The two will never be the same sign. In most cases, the Size property will be positive. When you launch the common font dialog box, the size listbox contains the value that is assigned to the Size property of TFont. Since the size listbox always displays positive values, Size ends up being positive, and Height ends up negative.

Note: Because the Size and Height properties can be negative, be careful not to write code that malfunctions if the value is less than zero. For example, the following code appears to increase the size of a font by 2 pixels every time a button is clicked. Because the Height property usually starts out negative, the font actually decreases in size instead of increasing. The label does start to grow when Height passes through 0 and becomes positive.

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        Label1->Font->Height = Label1->Font->Height + 2;
    }


Copyright © 1997-2000 by Harold Howe.
All rights reserved.