Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using the WinForms designer, do I always need to have DPI set to 96?

Tags:

winforms

With my current monitor I prefer a DPI setting of 120 pixels per inch (which windows suggests as the default). However, after designing a form, it often lays out incorrectly on systems that don't use 120 pixels per inch.

I'm wondering, is it necessary that I should set my display settings to 96 pixels per inch for whenever I use the designer?

Also, there are some problems when other developers have different DPIs. They open a form in the designer and move something like a text edit control, and suddenly find that it automatically resizes itself too. Then, there's one control that's a different size to the others and we're in a mess.

P.S. I've read related posts. They're all interesting, but didn't answer my question.

How to control the font DPI in .NET WinForms app

C# WinForms disable DPI scaling

WinForms Different DPI Layouts

DPI not scaling properly

Visual Studio and DPI issue

like image 888
Scott Langham Avatar asked Sep 12 '12 11:09

Scott Langham


People also ask

What is the default Windows DPI?

By default Windows has setting of 96 DPI.

How to start Visual Studio with 100% scaling?

If you've closed the informational bar at the top of the designer, but you still want to replicate the behavior of the link that says Restart Visual Studio with 100% scaling, you still can. Select Tools > Command Line > Developer Command Prompt from the Visual Studio menu bar. Then, enter devenv /noScale .

What is DPI C#?

DPI stands for Dots Per Inch is the number of pixels or points rendered in one inch on the interface.

What is DPI awareness?

The DPI Awareness Mode specifies the way an application is displayed when shown on a high resolution screen. Unaware. This mode works on all Windows versions prior to Windows Vista. In this mode, applications assume that all displays have the default 96 DPI (100%) scaling.


1 Answers

No. You don't need to always have the DPI set to 96 when using the WinForms designer.

If you set the AutoScaleMode property to Dpi then the designer will write the current system DPI into the designer.cs file in the AutoScaleDimensions property for the form. When the designer is used on a system with a different DPI, this information will be used to rescale the form and the designer can be used at a different DPI.

When I tried other scaling modes, this didn't seem to work well. 'None' meant that controls wouldn't scale at runtime, 'Font' seemed to suffer from rounding errors and when the display settings DPI changed, the control sizes could change slightly causing errors.

I also found that for UserControls that are added to forms it is best to set their AutoScaleMode to Inherit. If you use Dpi, then the controls on it get re-scaled twice and will end up being laid out incorrectly.


I came up with the guidelines above after a few hours of experimentation and internet searching where I found the following two articles:

Automatic scaling in Windows Forms

and:

Child controls on a UserControl may get clipped in a system with a lower Font Dpi

like image 71
Scott Langham Avatar answered Sep 29 '22 00:09

Scott Langham