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 thenamespace 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 to respond to changes in font size.
To use the desktop font and respond to font scheme changes
Create your form, and add the controls you want to it. For more information, seeand .
Add a reference to the Microsoft.Win32 namespace to your code.
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.
Implement a handler for theevent that causes the form to scale automatically when the category changes.
Finally, implement a handler for theevent that detaches the UserPreferenceChanged event handler.
Failure to include this code will cause your application to leak memory.
To manually change the font scheme in Windows XP
While your Windows Forms application is running, right-click the Windows desktop and choose Properties from the shortcut menu.
In the Display Properties dialog box, click the Appearance tab.
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.
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.