Home >Backend Development >C++ >How Can I Write Auto-Scaling WinForms Code That Adapts to Different DPI and Font Settings?

How Can I Write Auto-Scaling WinForms Code That Adapts to Different DPI and Font Settings?

Barbara Streisand
Barbara StreisandOriginal
2025-01-19 08:32:09174browse

How Can I Write Auto-Scaling WinForms Code That Adapts to Different DPI and Font Settings?

WinForms Auto Scaling Code Writing Guide

Introduction

WinForms does not lack powerful auto-scaling capabilities. .NET 2.0 and later versions have significant improvements in auto-scaling. This article explores design guidelines and limitations to help developers write WinForms code that adapts to various system fonts and DPI settings.

Zoom supports limited controls

Some WinForms controls have limitations with autoscaling:

  • Label control, if the AutoSize property is set to False and the Font property is inherited. Please explicitly set the font to bold in the properties window.
  • ListView The column width of the control. Override the form.ScaleControl method to adjust column widths.
  • SplitContainer Panel1MinSize, Panel2MinSize and SplitterDistance properties of the control.
  • TextBox control, if the MultiLine property is set to True and inherits the Font property. Please set the font explicitly.
  • ToolStripButton Image of the control. Manipulate the ToolStrip.AutoSize and ToolStrip.ImageScalingSize properties in the form's constructor.
  • PictureBox The SizeMode property of the control must be set to Zoom or StretchImage.
  • TreeView Image of the control. Adjust the ImageList.ImageSize property according to DPI.
  • Form The size of the control. Manually scale a fixed-size form after creation.

Auto-scaling design guidelines

To ensure effective autoscaling, please follow these guidelines:

  • Set AutoScaleMode to Font: All container controls should have AutoScaleMode set to Font to accommodate DPI and system font changes.
  • Set AutoScaleDimensions: Use AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);, assuming 96dpi and MS Sans Serif font. Please adjust these values ​​according to the selected DPI and font.
  • Designing at 96dpi: Avoid applying the Anchor Right or Bottom properties directly to user controls. Please use Panel as an intermediate.
  • Handling dynamic control additions: For dynamically added controls, pause layout, set AutoScaleMode and AutoScaleDimensions, and then resume layout before adding.
  • Avoid using Form.MaxSize in the designer: Set MaxSize dynamically to prevent scaling limitations.
  • Use Anchoring or Docking: Make sure all controls within the container use Anchoring or Docking for consistent autoscaling.
  • Limit form size in the designer: Keep the designer form size below 1000x680 for optimal scaling at various DPI settings.

The above is the detailed content of How Can I Write Auto-Scaling WinForms Code That Adapts to Different DPI and Font 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