Partager 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.

    Imports Microsoft.Win32
    
    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.

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
    
        ' Add any initialization after the InitializeComponent() call.
        AddHandler SystemEvents.UserPreferenceChanged, New UserPreferenceChangedEventHandler(AddressOf SystemEvents_UserPreferenceChangesEventHandler)
    End Sub
    
    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.

    Private Sub SystemEvents_UserPreferenceChangesEventHandler(ByVal sender As Object, ByVal e As UserPreferenceChangedEventArgs)
        If (e.Category = UserPreferenceCategory.Window) Then
            Me.Font = SystemFonts.IconTitleFont
        End If
    End Sub
    
    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.

NoteImportant

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

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    RemoveHandler SystemEvents.UserPreferenceChanged, New UserPreferenceChangedEventHandler(AddressOf SystemEvents_UserPreferenceChangesEventHandler)
End Sub
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
}

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 will 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

The constructer 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

Reference

PerformAutoScale

Concepts

Automatic Scaling in Windows Forms