Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Automatic scaling lets a form and its controls, designed on one machine with a certain display resolution or font, display appropriately on another machine with a different display resolution or font. Forms and controls intelligently resize to stay consistent with native windows and other applications on both users' and other developers' machines. Automatic scaling and visual styles help Windows Forms applications maintain a consistent look when compared to native Windows applications on each user's machine.
For the most part, automatic scaling works as expected in Windows Forms. However, font scheme changes can be problematic. For an example of how to resolve this issue, see How to: Respond to Font Scheme Changes in a Windows Forms Application.
DPI awareness in .NET
DPI awareness is configured through the project file using the ApplicationHighDpiMode property, which is set to SystemAware by default. This configuration works with the application bootstrap system to automatically configure DPI handling when your application starts.
The default and recommended DPI mode for Windows Forms applications is SystemAware. This mode queries for the DPI setting once at application startup and uses that value throughout the application's lifetime, providing consistent scaling behavior.
Configure DPI mode in your project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ApplicationHighDpiMode>SystemAware</ApplicationHighDpiMode>
</PropertyGroup>
</Project>
Note
While the recommended approach is to configure DPI through the project file, you can still use an application manifest file (app.manifest) to override these settings. However, using the manifest is discouraged because it can cause conflicts with the application configuration. For more information, see Compiler Warning WFO0003.
Visual Studio designer considerations
When designing forms in Visual Studio, you might need to configure the designer's DPI awareness separately from your application. Starting with Visual Studio 2022 version 17.8, you can set the ForceDesignerDPIUnaware property in your project file to make the Windows Forms designer run in DPI-unaware mode. This setting helps avoid rendering issues when designing forms on high-DPI monitors:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ForceDesignerDPIUnaware>true</ForceDesignerDPIUnaware>
</PropertyGroup>
</Project>
This property only affects the Visual Studio designer and doesn't change how your application runs. For more information, see Disable DPI-awareness to address scaling issues.
Available DPI modes include:
SystemAware—The application queries for the DPI once at startup and uses this value for the lifetime of the app.PerMonitor—The application checks for DPI changes on a per-monitor basis and rescales when DPI changes.PerMonitorV2—Similar toPerMonitor, but enables child window DPI change notifications and improved scaling behavior.DpiUnaware—The application doesn't scale for DPI changes, and Windows handles the scaling.DpiUnawareGdiScaled—Similar toDpiUnawarebut provides better quality for GDI-based content.
For more information about configuring DPI settings, see What's new in Windows Forms for .NET 6.
Need for automatic scaling
Without automatic scaling, an application designed for one display resolution or font appears too small or too large when that resolution or font changes. For example, if you design an application using Tahoma 9 point as a baseline, without adjustment it appears too small when run on a machine where the system font is Tahoma 12 point. Text elements, such as titles, menus, and text box contents, render smaller than other applications. Furthermore, the size of user interface (UI) elements that contain text, such as the title bar, menus, and many controls, depend on the font used. In this example, these elements also appear relatively smaller.
An analogous situation occurs when you design an application for a certain display resolution. The most common display resolution is 96 dots per inch (DPI), which equals 100% display scaling, but higher resolution displays supporting 125%, 150%, 200% (which respectively equal 120, 144, and 192 DPI) and above are becoming more common. Without adjustment, an application, especially a graphics-based one, designed for one resolution appears either too large or too small when run at another resolution.
Automatic scaling addresses these problems by automatically resizing the form and its child controls according to the relative font size or display resolution. The Windows operating system supports automatic scaling of dialog boxes using a relative unit of measurement called dialog units. A dialog unit is based on the system font, and its relationship to pixels can be determined through the Win32 SDK function GetDialogBaseUnits. When a user changes the theme used by Windows, all dialog boxes automatically adjust accordingly. Windows Forms supports automatic scaling either according to the default system font or the display resolution. Optionally, you can disable automatic scaling in an application.
Caution
Arbitrary mixtures of DPI and font scaling modes aren't supported. Although you can scale a user control using one mode (for example, DPI) and place it on a form using another mode (Font) with no issues, mixing a base form in one mode and a derived form in another can lead to unexpected results.
Automatic scaling in action
Windows Forms uses the following logic to automatically scale forms and their contents:
At design time, each ContainerControl records the scaling mode and its current resolution in the AutoScaleMode and AutoScaleDimensions properties, respectively.
At run time, the actual resolution is stored in the CurrentAutoScaleDimensions property. The AutoScaleFactor property dynamically calculates the ratio between the run-time and design-time scaling resolution.
When the form loads, if the values of CurrentAutoScaleDimensions and AutoScaleDimensions differ, then the PerformAutoScale method is called to scale the control and its children. This method suspends layout and calls the Scale method to perform the actual scaling. Afterward, the value of AutoScaleDimensions is updated to avoid progressive scaling.
PerformAutoScale is also automatically invoked in the following situations:
In response to the OnFontChanged event if the scaling mode is Font.
When the layout of the container control resumes and a change is detected in the AutoScaleDimensions or AutoScaleMode properties.
When a parent ContainerControl is being scaled. Each container control is responsible for scaling its children using its own scaling factors and not the one from its parent container.
Child controls can modify their scaling behavior through several means:
Override the ScaleChildren property to determine if their child controls should be scaled.
Override the GetScaledBounds method to adjust the bounds that the control is scaled to, but not the scaling logic.
Override the ScaleControl method to change the scaling logic for the current control.
High DPI improvements
.NET includes significant improvements to high DPI rendering, especially with PerMonitorV2 mode:
- Per-monitor DPI awareness—Applications dynamically adjust when moved between monitors with different DPI settings.
- Improved scaling behavior—Controls scale correctly when DPI changes, including nested controls and container controls (.NET 6+).
- Form size scaling—MaximumSize and MinimumSize properties scale based on the current monitor DPI settings (.NET 7+, enabled by default in .NET 8+).
- DPI change events—New events let you programmatically handle dynamic DPI changes:
- DpiChanged—Fires when the DPI setting changes on the display device where the form is currently displayed.
- DpiChangedBeforeParent—Fires when the DPI setting for a control changes programmatically before a DPI change event for its parent control or form occurs.
- DpiChangedAfterParent—Fires when the DPI setting for a control changes programmatically after a DPI change event for its parent control or form occurs.
For more information about high DPI improvements, see What's new in Windows Forms for .NET 6, What's new in Windows Forms for .NET 7, and What's new in Windows Forms for .NET 8.
.NET Framework differences
.NET Framework and .NET handle DPI awareness differently:
- In .NET Framework, configure DPI awareness through an app.config file with the
<System.Windows.Forms.ApplicationConfigurationSection>element. - In .NET, configure DPI awareness through the project file with the
ApplicationHighDpiModeproperty. - .NET Framework uses manifest files for DPI configuration, which is no longer recommended.
- .NET provides better scaling behavior and more reliable DPI change handling.
For information about .NET Framework DPI configuration, see High DPI support in Windows Forms.
See also
.NET Desktop feedback