Home > Backend Development > C++ > How Can I Optimize WinForms Control Scaling for Auto-Adjustment in High-DPI Environments?

How Can I Optimize WinForms Control Scaling for Auto-Adjustment in High-DPI Environments?

Mary-Kate Olsen
Release: 2025-01-19 08:26:10
Original
447 people have browsed it

How Can I Optimize WinForms Control Scaling for Auto-Adjustment in High-DPI Environments?

WinForms control scaling: for optimal automatic adjustment in high DPI environments

Although it is generally believed that WinForms has limited autoscaling capabilities in high DPI environments, in fact, .NET 2.0 and later have significantly improved its autoscaling capabilities. However, to take full advantage of these improvements, developers must follow certain design guidelines.

Controls that do not support auto-scaling

Some controls have limitations in scaling:

  • Label: If the AutoSize attribute is disabled and the font is an inherited font, set the Font attribute explicitly to prevent scaling errors.
  • ListView: Override the form's ScaleControl method to handle column width adjustments.
  • SplitContainer: Avoid setting the Panel1MinSize, Panel2MinSize and SplitterDistance attributes.
  • TextBox: If the MultiLine attribute is enabled and the font is an inherited font, set the Font attribute explicitly.
  • ToolStripButton: Adjusts CreateGraphics.DpiX based on .DpiY and ToolStrip.ImageScalingSize.
  • PictureBox: Make sure the SizeMode attribute is set to Zoom or StretchImage.
  • TreeView: Sets CreateGraphics.DpiX based on .DpiY and ImageList.ImageSize.
  • Form: Manually scale the form after creating a fixed size form.

Design Guidelines

1. Container control

  • Sets the AutoScaleMode property of all container controls to Font.
  • Assuming 96dpi and the default MS Sans Serif font, assign the AutoScaleDimensions attribute to new System.Drawing.SizeF(6F, 13F) (adjust if necessary).

2. Designer configuration

  • Design at 96dpi. Override high DPI scaling behavior to "System" in Visual Studio.
  • Make sure the container control's AutoSize property is enabled.

3. Font management

  • Set the Font property only at the leaf control level or in the constructor of the base form (for application-wide defaults).
  • Changing Font in the base form will recalculate AutoScaleDimensions; update the designer file accordingly.

4. Docking and anchoring

  • Avoid anchoring controls directly to UserControls; use a Panel or other container instead.
  • Use the Dock attribute instead of Anchor Right or Bottom.

5. Dynamically added controls

  • Pause layout before adding dynamic controls, set AutoScaleDimensions and AutoScaleMode, then resume layout.

6. Base class and AutoScale

  • Inherited base classes should have their AutoScaleMode attribute set to Inherit. Setting it to any other value may disable automatic scaling.

7. Window size limit

  • Avoid using static MaxSize attributes. Set OnLoad and MinSize to the scaled dimensions in the constructor or MaxSize event.
  • Make sure the size of the form does not exceed 1000x680 ClientSize (994x642 in the designer).

The above is the detailed content of How Can I Optimize WinForms Control Scaling for Auto-Adjustment in High-DPI Environments?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template