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:
AutoSize
attribute is disabled and the font is an inherited font, set the Font
attribute explicitly to prevent scaling errors. ScaleControl
method to handle column width adjustments. Panel1MinSize
, Panel2MinSize
and SplitterDistance
attributes. MultiLine
attribute is enabled and the font is an inherited font, set the Font
attribute explicitly. CreateGraphics.DpiX
based on .DpiY
and ToolStrip.ImageScalingSize
. SizeMode
attribute is set to Zoom
or StretchImage
. CreateGraphics.DpiX
based on .DpiY
and ImageList.ImageSize
. Design Guidelines
1. Container control
AutoScaleMode
property of all container controls to Font
. AutoScaleDimensions
attribute to new System.Drawing.SizeF(6F, 13F)
(adjust if necessary). 2. Designer configuration
AutoSize
property is enabled. 3. Font management
Font
property only at the leaf control level or in the constructor of the base form (for application-wide defaults). Font
in the base form will recalculate AutoScaleDimensions
; update the designer file accordingly. 4. Docking and anchoring
Dock
attribute instead of Anchor Right
or Bottom
. 5. Dynamically added controls
AutoScaleDimensions
and AutoScaleMode
, then resume layout. 6. Base class and AutoScale
AutoScaleMode
attribute set to Inherit
. Setting it to any other value may disable automatic scaling. 7. Window size limit
MaxSize
attributes. Set OnLoad
and MinSize
to the scaled dimensions in the constructor or MaxSize
event. 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!