I created an app for a small business. Some of the employees in the office can not see the form correctly. The reason is they have their DPI setting set to above 96dpi. Does anybody know of a way to control this?
For all of you who have experience with winforms apps, how do you control your form layout so that DPI does not affect the look of the application?
Best Answer
Assuming you do not try to honor the user's UI font choice (SystemFonts.IconTitleFont), and hard-code your forms for one font size only (e.g. Tahoma 8pt, Microsoft Sans Serif 8.25pt), you can set your form's
AutoScaleMode
toScaleMode.Dpi
.This will scale the size of the form and most of it child controls by the factor
CurrentDpiSetting / 96
by callingForm.Scale()
, which in turns calls the protectedScaleControl()
method recursivly on itself and all child controls.ScaleControl
will increase a control's position, size, font, etc as needed for the new scaling factor.This is all well and good if you're just using controls. But if you ever use any hard-coded pixel sizes, you'll need to scale your pixel widths and lengths by the current scale factor of the form. Some examples of situations that could have hard-coded pixel sizes:
If this is the case, you'll need to scale those hard-coded values by the "current scaling factor". Unfortunatly the "current" scale factor is not provided, we need to record it ourselves. The solution is to assume that initially the scaling factor is 1.0 and each time
ScaleControl()
is called, modify the running scale factor by the new factor.Initially the scaling factor is
1.0
. If form is then scaled by1.25
, the scaling factor then becomes:If the form is then scaled by
0.95
, the new scaling factor becomesThe reason a
SizeF
is used (rather than a single floating point value) is that scaling amounts can be different in the x and y directions. If a form is set toScaleMode.Font
, the form is scaled to the new font size. Fonts can have different aspect ratios (e.g. Segoe UI is taller font than Tahoma). This means you have to scale x and y values independantly.So if you wanted to place a control at location
(11,56)
, you would have to change your positioning code from:to
The same applies if you were going to pick a font size:
would have to become:
And extracting a 32x32 icon to a bitmap would change from:
to
etc.
Supporting non-standard DPI displays is a tax that all developers should pay. But the fact that nobody wants to is why Microsoft gave up and added to Vista the ability for the graphics card to stretch any applications that don't say they properly handle high-dpi.