8

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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Scott Langham
  • 58,735
  • 39
  • 131
  • 204
  • 1
    By all means, figure out how to get layout right by constantly changing the DPI. This is definitely going to happen after you ship it. You would only consider keeping it at a set value if you prefer sticking your head in the sand. – Hans Passant Sep 12 '12 at 12:01
  • 2
    I do change the DPI to test layouts with different settings. But.... should I stick to one DPI when using the designer? It seems if different DPI settings are used with the designer, it will re-lay out controls every time you edit a form with a different DPI, subtly moving them around meaning it's hard to get consistent results. Eee. perhaps I have answered my own question. I might stick with one DPI for when using the designer. I just kinda wondered if everybody else was already doing that or had some other approach. – Scott Langham Sep 12 '12 at 12:13

2 Answers2

5

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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Scott Langham
  • 58,735
  • 39
  • 131
  • 204
1

I don't think setting your dpi to a different value permanently will help you. The problem is that there are problems when you change the dpi, i.e. the form layout you have isn't able to deal with different dpi's.

I don't have an absolute solution for you, except that you should test with different dpis and see if it produces problems with the form display. It isn't hard to work out what causes problems and you'll learn what to avoid fairly quickly.

kristianp
  • 5,496
  • 37
  • 56
  • 1
    I've been doing WinForm development for some time, and I haven't learnt what to avoid yet. :) – Scott Langham Sep 12 '12 at 12:00
  • What I'm saying is if you test with different dpi's you'll soon be able to get your forms to deal better with different dpi settings. – kristianp Sep 12 '12 at 12:01