Controls which do not support scaling properly:
LabelwithAutoSize = FalseandFontinherited. Explicitly setFonton the control so it appears in bold in the Properties window.ListViewcolumn widths don’t scale. Override the form’sScaleControlto do it instead. See this answerSplitContainer‘sPanel1MinSize,Panel2MinSizeandSplitterDistancepropertiesTextBoxwithMultiLine = TrueandFontinherited. Explicitly setFonton the control so it appears in bold in the Properties window.-
ToolStripButton‘s image. In the form’s constructor:- Set
ToolStrip.AutoSize = False - Set
ToolStrip.ImageScalingSizeaccording toCreateGraphics.DpiXand.DpiY - Set
ToolStrip.AutoSize = Trueif needed.
Sometimes
AutoSizecan be left atTruebut sometimes it fails to resize without those steps. Works without that changes with .NET Framework 4.5.2 andEnableWindowsFormsHighDpiAutoResizing. - Set
TreeView‘s images. SetImageList.ImageSizeaccording toCreateGraphics.DpiXand.DpiY. ForStateImageList, works without that changes with .NET Framework 4.5.1 andEnableWindowsFormsHighDpiAutoResizing.Form‘s size. Scale fixed sizeForm‘s manually after creation.
Design Guidelines:
-
All ContainerControls must be set to the same
AutoScaleMode = Font.
(Font will handle both DPI changes and changes to the system font
size setting; DPI will only handle DPI changes, not changes to the
system font size setting.) -
All ContainerControls must also be set with the same
AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);, assuming 96dpi (see the next bullet) and default Font of MS Sans Serif (see the bullet two down). That is auto-added by the designer
based on the DPI you open the designer in… but was missing from
many of our oldest designer files. Perhaps Visual Studio .NET (the
version before VS 2005) was not adding that in properly. -
Do all your designer work in 96dpi (we might be able to switch to
120dpi; but the wisdom on the internet says to stick to 96dpi;
experimentation is in order there; by design, it shouldn’t matter as it just changes theAutoScaleDimensionsline that the designer inserts).
To set Visual Studio to run at a virtual 96dpi on a high-resolution display,
find its .exe file, right-click to edit properties, and under Compatibility
select “Override high DPI scaling behavior. Scaling performed by: System”. -
Be sure you never set the Font at the container level… only on the
leaf controls OR in the constructor of your most base Form if you want an application-wide default Font other than MS Sans Serif. (Setting the Font on a Container seems to turn off
the auto-scaling of that container because it alphabetically comes after the setting of AutoScaleMode and AutoScaleDimensions settings.) NOTE that if you do change the Font in your most base Form’s constructor, that will cause your AutoScaleDimensions to compute differently than 6×13; in particular, if you change to Segoe UI (the Win 10 default font), then it will be 7×15… you will need to touch every Form in the Designer so that it can recompute all the dimensions in that .designer file, including theAutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);. -
Do NOT use Anchor
RightorBottomanchored to a UserControl… its
positioning will not auto-scale; instead, drop a Panel or other
container into your UserControl and Anchor your other Controls to
that Panel; have the Panel use DockRight,Bottom, orFillin your
UserControl. -
Only the controls in the Controls lists when
ResumeLayoutat the end
ofInitializeComponentis called will be auto-scaled… if you
dynamically add controls, then you need toSuspendLayout();
AutoScaleDimensions = new SizeF(6F, 13F);AutoScaleMode = AutoScaleMode.Font;
ResumeLayout();on that control before you add it in. And your
positioning will also need to be adjusted if you are not using Dock
modes or a Layout Manager likeFlowLayoutPanelorTableLayoutPanel. -
Base classes derived from
ContainerControlshould leaveAutoScaleModeset toInherit(the default value set in classContainerControl; but NOT the default set by the designer). If you set it to anything else, and then your derived class tries to set it to Font (as it should), then the act of setting that toFontwill clear out the designer’s setting ofAutoScaleDimensions, resulting in actually toggling off auto-scaling! (This guideline combined with the prior one means you can never instantiate base classes in a designer… all classes need to either be designed as base classes or as leaf classes!) -
Avoid using
Form.MaxSizestatically / in the Designer.MinSizeandMaxSizeon Form do not scale as much as everything else. So, if you do all your work in 96dpi, then when at higher DPI yourMinSizewon’t cause problems, but may not be as restrictive as you expected, but yourMaxSizemay limit your Size’s scaling, which can cause problems. If you wantMinSize == Size == MaxSize, don’t do that in the Designer… do that in your constructor orOnLoadoverride… set bothMinSizeandMaxSizeto your properly-scaled Size. -
All of the Controls on a particular
PanelorContainershould either use Anchoring or Docking. If you mix them, the auto-scaling done by thatPanelwill often misbehave in subtle bizarre ways. -
When it does its auto-scaling, it will be trying to scale the overall Form… however, if in that process it runs into the upper limit of the screen size, that is a hard limit that can then screw up (clip) the scaling. Therefore, you should make sure all Forms in the Designer at 100%/96dpi are sized no larger than 1024×720 (which corresponds to 150% on a 1080p screen or 300% which is the Windows recommended value on a 4K screen). But you need to subtract out for the giant Win10 title/caption bar… so more like 1000×680 max Size… which in the designer will be like 994×642 ClientSize. (So, you can do a FindAll References on ClientSize to find violators.)