다음을 통해 공유


Automatic Scaling in Windows Forms

Automatic scaling enables a form and its controls, designed on one machine with a certain display resolution or system font, to be displayed appropriately on another machine with a different display resolution or system 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. The support of the .NET Framework for automatic scaling and visual styles enables .NET Framework applications to maintain a consistent look and feel when compared to native Windows applications on each user's machine.

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), but higher resolution displays supporting 120, 133, 170, 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 ameliorate 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,

The .NET Framework supports automatic scaling either according to the default system font or the display resolution. Optionally, automatic scaling can be disabled in an application.

Original Support for Automatic Scaling

Versions 1.0 and 1.1 of the .NET Framework supported automatic scaling in a straightforward manner that was dependent on the Windows default font used for the UI, represented by the Win32 SDK value DEFAULT_GUI_FONT. This font is typically only changed when the display resolution changes. The following mechanism was used to implement automatic scaling:

  1. At design time, the AutoScaleBaseSize property (which is now deprecated) was set to the height and width of the default system font on the developer's machine.

  2. At runtime, the default system font of the user's machine was used to initialize the Font property of the Form class.

  3. Before displaying the form, the ApplyAutoScaling method was called to scale the form. This method calculated the relative scale sizes from AutoScaleBaseSize and Font then called the Scale method to actually scale the form and its children.

  4. The value of AutoScaleBaseSize was updated so that subsequent calls to ApplyAutoScaling did not progressively resize the form.

While this mechanism was sufficient for most purposes, it suffered from the following limitations:

  • Since the AutoScaleBaseSize property represents the baseline font size as integral values, rounding errors occur that become evident when a form is cycled through multiple resolutions.

  • Automatic scaling was implemented in only the Form class, not in the ContainerControl class. As a result, user controls would scale correctly only when the user control was designed at the same resolution as the form, and it was placed in the form at design time.

  • Forms and their child controls could only be concurrently designed by multiple developers if their machine resolutions were the same. Likewise it also made inheritance of a form dependent on the resolution associated with the parent form.

  • It is not compatible with the newer layout managers introduced with the .NET Framework version 2.0, such as FlowLayoutPanel and TableLayoutPanel.

  • It did not support scaling based directly on the display resolution that is required for compatibility to the .NET Compact Framework.

Although this mechanism is preserved in the .NET Framework version 2.0 to maintain backward compatibility, it has been superseded by the more robust scaling mechanism described next. As a consequence, the AutoScale, ApplyAutoScaling, AutoScaleBaseSize, and certain Scale overloads are marked as obsolete.

NoteNote

You can safely delete references to these members when you upgrade your legacy code to the .NET Framework version 2.0.

Current Support for Automatic Scaling

The .NET Framework version 2.0 surmounts previous limitations by introducing the following changes to the automatic scaling of Windows Forms:

  • Base support for scaling has been moved to the ContainerControl class so that forms, native composite controls and user controls all receive uniform scaling support. The new members AutoScaleFactor, AutoScaleDimensions, AutoScaleMode and PerformAutoScale have been added.

  • The Control class also has several new members that allow it to participate in scaling and to support mixed scaling on the same form. Specifically the Scale, ScaleChildren, and GetScaledBounds members support scaling.

  • Support for scaling based upon the screen resolution has been added to complement system font support, as defined by the AutoScaleMode enumeration. This mode is compatible with automatic scaling supported by the .NET Compact Framework enabling easier application migration.

  • Compatibility with layout managers such as FlowLayoutPanel and TableLayoutPanel has been added to the implementation of automatic scaling.

  • Scaling factors are now represented as floating point values, typically using the SizeF structure, so that rounding errors have been practically eliminated.

Caution noteCaution

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

Tasks

How to: Enable Windows XP Visual Styles
How to: Improve Performance by Avoiding Automatic Scaling

Reference

AutoScaleMode
Scale
PerformAutoScale
AutoScaleDimensions

Concepts

Rendering Controls with Visual Styles