BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

System Information
.NET 1.1+

Detecting Font Installation

Applications that work with fonts, such as word processors or desktop publishing software, should detect when the installed fonts are changed. This ensures that new fonts are immmediately available and removed fonts cannot be used.

System Events

If you are creating an application that works with formatted text, it is likely that you will allow the user to change the typeface, or font. As users have the ability to install additional typefaces, or remove standard ones, you should generally obtain the list of installed fonts and let the user choose which they wish to work with.

It is possible that a user will install or remove a font from Windows whilst your application is running. If a font is added, it should not be necessary to close and re-open your program in order to use it. You could ensure that the new typeface is visible to the user by reading the font list every time they wish to change typeface. However, this might be inefficient.

If a font is removed from the operating system, it may be necessary to update any loaded documents to change any text that uses the deleted style. Constantly checking the list of installed fonts is not a sensible approach to tackle this problem.

A better way to detect when fonts are added or removed is to subscribe to a system event. The InstalledFontsChanged event of the SystemEvents class is designed for this purpose. It is raised whenever the font list is changed. You can subscribe to the event and read the font list when it is fired, updating selection lists and document fonts accordingly.

The following code, added to a console application, registers the InstalledFontsChanged event and outputs a message whenever a font is installed or deleted. It runs until the Enter key is pressed.

NB: As this is a static event it is important to unsubscribe from it when it is no longer required. Failing to do so can cause a memory leak.

static void Main(string[] args)
{
    SystemEvents.InstalledFontsChanged += SystemEvents_InstalledFontsChanged;
    Console.ReadLine();
    SystemEvents.InstalledFontsChanged -= SystemEvents_InstalledFontsChanged;
}

static void SystemEvents_InstalledFontsChanged(object sender, EventArgs e)
{
    Console.WriteLine("Installed Fonts Changed");
}
29 March 2014