Automatic scaling (Windows Forms .NET)

Automatic scaling enables a form and its controls, designed on one machine with a certain display resolution or font, to be displayed appropriately on another machine with a different display resolution or font. It assures that the form and its controls will intelligently resize to be consistent with native windows and other applications on both the users' and other developers' machines. Automatic scaling and visual styles enable Windows Forms applications to maintain a consistent look-and-feel 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.

Need for automatic scaling

Without automatic scaling, an application designed for one display resolution or font will either appear too small or too large when that resolution or font is changed. For example, if the application is designed using Tahoma 9 point as a baseline, without adjustment it will appear too small if run on a machine where the system font is Tahoma 12 point. Text elements, such as titles, menus, text box contents, and so on will 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 are dependent on the font used. In this example, these elements will also appear relatively smaller.

An analogous situation occurs when an application is designed 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 will appear either too large or too small when run at another resolution.

Automatic scaling seeks to address 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 though the Win32 SDK function GetDialogBaseUnits. When a user changes the theme used by Windows, all dialog boxes are automatically adjusted accordingly. In addition, Windows Forms supports automatic scaling either according to the default system font or the display resolution. Optionally, automatic scaling can be disabled in an application.

Caution

Arbitrary mixtures of DPI and font scaling modes are not supported. Although you may scale a user control using one mode (for example, DPI) and place it on a form using another mode (Font) with no issues, but 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:

  1. At design time, each ContainerControl records the scaling mode and it current resolution in the AutoScaleMode and AutoScaleDimensions, respectively.

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

  3. When the form loads, if the values of CurrentAutoScaleDimensions and AutoScaleDimensions are different, 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. Afterwards, the value of AutoScaleDimensions is updated to avoid progressive scaling.

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

    • As implied above, 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.

  5. Child controls can modify their scaling behavior through several means:

    • The ScaleChildren property can be overridden to determine if their child controls should be scaled or not.

    • The GetScaledBounds method can be overridden to adjust the bounds that the control is scaled to, but not the scaling logic.

    • The ScaleControl method can be overridden to change the scaling logic for the current control.

See also