Edit

Share via


How to: Respond to Font Scheme Changes in a Windows Forms Application

In the Windows operating systems, a user can change the system-wide font settings to make the default font appear larger or smaller. Changing these font settings is critical for users who are visually impaired and require larger type to read the text on their screens. You can adjust your Windows Forms application to react to these changes by increasing or decreasing the size of the form and all contained text whenever the font scheme changes. If you want your form to accommodate changes in font sizes dynamically, you can add code to your form.

Typically, the default font used by Windows Forms is the font returned by the Microsoft.Win32 namespace call to GetStockObject(DEFAULT_GUI_FONT). The font returned by this call only changes when the screen resolution changes. As shown in the following procedure, your code must change the default font to IconTitleFont to respond to changes in font size.

To use the desktop font and respond to font scheme changes

  1. Create your form, and add the controls you want to it. For more information, see How to: Create a Windows Forms Application from the Command Line and Controls to Use on Windows Forms.

  2. Add a reference to the Microsoft.Win32 namespace to your code.

    using Microsoft.Win32;
    
  3. Add the following code to the constructor of your form to hook up required event handlers, and to change the default font in use for the form.

    this.Font = SystemFonts.IconTitleFont;
    SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
    this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
    
  4. Implement a handler for the UserPreferenceChanged event that causes the form to scale automatically when the Window category changes.

    void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
    {
        if (e.Category == UserPreferenceCategory.Window)
        {
            this.Font = SystemFonts.IconTitleFont;
        }
    }
    
  5. Finally, implement a handler for the FormClosing event that detaches the UserPreferenceChanged event handler.

    Important

    Failure to include this code will cause your application to leak memory.

    void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
    }
    
  6. Compile and run the code.

To manually change the font scheme in Windows XP

  1. While your Windows Forms application is running, right-click the Windows desktop and choose Properties from the shortcut menu.

  2. In the Display Properties dialog box, click the Appearance tab.

  3. From the Font Size drop-down list box, select a new font size.

    You'll notice that the form now reacts to run-time changes in the desktop font scheme. When the user changes between Normal, Large Fonts, and Extra Large Fonts, the form changes font and scales correctly.

Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace WinFormsAutoScaling
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.Font = SystemFonts.IconTitleFont;
            SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
        }

        void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
        {
            if (e.Category == UserPreferenceCategory.Window)
            {
                this.Font = SystemFonts.IconTitleFont;
            }
        }

        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
        }
    }
}

The constructor in this code example contains a call to InitializeComponent, which is defined when you create a new Windows Forms project in Visual Studio. Remove this line of code if you are building your application on the command line.

See also