* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
4.6 KiB
title, titleSuffix, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | titleSuffix | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||
|---|---|---|---|---|---|---|---|---|
| Respond to Font Scheme Changes in a Windows Forms app | 03/30/2017 |
|
|
4db27702-22e7-43bf-a07d-9a004549853c |
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 xref: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 xref:System.Drawing.SystemFonts.IconTitleFont%2A 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, see How to: Create a Windows Forms Application from the Command Line and Controls to Use on Windows Forms.
-
Add a reference to the xref:Microsoft.Win32 namespace to your code.
[!code-csharpWinFormsAutoScaling#2] [!code-vbWinFormsAutoScaling#2]
-
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.
[!code-csharpWinFormsAutoScaling#3] [!code-vbWinFormsAutoScaling#3]
-
Implement a handler for the xref:Microsoft.Win32.SystemEvents.UserPreferenceChanged event that causes the form to scale automatically when the xref:Microsoft.Win32.UserPreferenceCategory.Window category changes.
[!code-csharpWinFormsAutoScaling#4] [!code-vbWinFormsAutoScaling#4]
-
Finally, implement a handler for the xref:System.Windows.Forms.Form.FormClosing event that detaches the xref:Microsoft.Win32.SystemEvents.UserPreferenceChanged event handler.
Important
Failure to include this code will cause your application to leak memory.
[!code-csharpWinFormsAutoScaling#5] [!code-vbWinFormsAutoScaling#5]
-
Compile and run the code.
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'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
[!code-csharpWinFormsAutoScaling#1] [!code-vbWinFormsAutoScaling#1]
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.