Home Articles Books Downloads FAQs Tips

Q: Find out which fonts are installed on a computer


Answer

The API contains a host of EnumFontXXXX that allow you to query the OS for many different font properties. Usually you just want to know if a system supports a specific font, such as Courier or Times New Roman. If this is the case, the easiest way to determine if a system supports a font is to check the Fonts property of the global Screen object. The TPrinter class has a similar Fonts property.

The following code example fills a listbox with all of the fonts that are installed on a machine. When the user selects an item from the listbox, the font of a label changes to match.

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    // fill the listbox with the strings from the
    // Fonts property of the global screen object
    ListBox1->Items->Assign(Screen->Fonts);
    ListBox1->ItemIndex = 0;
}
//------------------------------------------------------------
void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
    // Change the font of a label based on the listbox selection
    Label1->Font->Name = ListBox1->Items->Strings[ListBox1->ItemIndex];
}

The Fonts property of TScreen is a TStringList. TStringList provides an IndexOf method that searches the list for a specific string. The following code shows how to use the IndexOf method to determine if a font is installed.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    // IndexOf returns the index of the string in the list if
    // the string is found. If not found, it returns -1
    if(Screen->Fonts->IndexOf("Courier New") >= 0)
        ShowMessage("Courier New is installed");
    else
        ShowMessage("Courier New not found");
}


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