Home >Backend Development >C++ >How Can WinForms Controls Be Properly Auto-Scaled to System Font and DPI Settings?

How Can WinForms Controls Be Properly Auto-Scaled to System Font and DPI Settings?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-19 08:16:08823browse

How Can WinForms Controls Be Properly Auto-Scaled to System Font and DPI Settings?

WinForms autoscaling: adapting to system fonts and DPI settings

Introduction

Previous reviews often suggested that WinForms' autoscaling capabilities are inferior to WPF's. However, based on the author's research and testing, WinForms' autoscaling functionality has improved significantly in .NET 2.0.

Zoom limit control

Despite improvements, some controls in WinForms still have scaling limitations and should be avoided:

  • AutoSize = False and inherit the font's Label: Explicitly set the control's font in the properties window so that it appears bold.
  • ListView column width: (override the form's ScaleControl to correct this problem)
  • Panel1MinSize, Panel2MinSize and SplitterDistance properties of SplitContainer
  • MultiLine = True and TextBox inheriting font: Explicitly sets the font of the control.
  • Image for ToolStripButton: Adjust ToolStrip.AutoSize and ToolStrip.ImageScalingSize in the form's constructor.
  • PictureBox.SizeMode must be set to Zoom or StretchImage.
  • Images for TreeView: Set ImageList.ImageSize based on CreateGraphics.DpiX and .DpiY.
  • Form size: Manually scale a fixed size form after creation.

Auto-scaling design guide

To ensure correct autoscaling in WinForms, the following guidelines must be followed:

  • Set all ContainerControls' AutoScaleMode to Font.
  • Set all ContainerControls' AutoScaleDimensions to 6F, 13F assuming 96dpi and default font (MS Sans Serif).
  • Work at 96dpi and edit properties to override Visual Studio's DPI scaling behavior.
  • Avoid setting fonts at the container level.
  • Dock controls to panels instead of directly to UserControls.
  • When adding controls dynamically, pause and resume layout.
  • Keep the AutoScaleMode of base classes derived from ContainerControl as Inherit.
  • Define MinSize and MaxSize in code, not in the designer.
  • Consistently use dock or anchor within a container.
  • Keep the form size below 1000x680 in the designer at 96dpi to avoid cropping when scaling.

The above is the detailed content of How Can WinForms Controls Be Properly Auto-Scaled to System Font and DPI Settings?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn