7

I have a project converted from .NET 1.1 to 3.5 being developed in 2008.

If I open the project on Windows 7, it converts the size of everything to 120dpi sizes. If I then open it with 96 dpi it changes back. Is there any way for me to develop so it looks good in both, and not have Visual Studio change the sizes if opened on a system with different DPI?

This question is perhaps better phrased/duplicated here: visual studio designer dpi setting

Or here: Visual Studio and DPI issue

To be clear what I am trying to do is prevent the control from being resized in the designer when being used by multiple developers with different DPI settings. In all cases where I've noticed this issue I've been working with a .NET 1 or .NET 2 project upgraded to use Visual Studio 2008 originally, now Visual Studio 2010.

Update (in case anyone wants to compete with @Ben for bonus points): I have a form which resizes correctly, but the size of the window gets changed programmatically. Because the designer isn't aware of the dimensions, the window resizes incorrectly if a developer using a different DPI touches the form. Is there a way of getting around that problem other than going back to not scaling the UI at runtime?

For instance: I have two sizes for my window declared, which work correctly if developers use 120 dpi at design time:

private static Size smallSize = new Size(960, 500);
private static Size largeSize = new Size(960, 615);

I'm just trying to wrap my head around what I would need to do to not have a horrible design regression if someone edits the form in a 96dpi designer.

Community
  • 1
  • 1
Kevin Stricker
  • 17,178
  • 5
  • 45
  • 71
  • I have not checked it but there is a compatibility mode in Windows 7 for applications. You can try tinkering with the settings. – PradeepGB Nov 02 '10 at 09:59

2 Answers2

11

AutoScaleMode = AutoScaleMode.None;

See also: ContainerControl.AutoScaleMode property

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • My understanding is that setting changes how controls are resized in the built application. What I am trying to do is prevent the control from being resized in the designer when being used by multiple developers with different DPI settings. – Kevin Stricker Jul 12 '11 at 02:38
  • @mootinator: If you still want the runtime resize behavior (but if it ruins your layout at design time, it'll be equally bad at runtime), a number of developers choose to iterate through the `Controls` collection in the constructor (after calling the designer-generated code) and change `AutoSizeMode` to the desired runtime setting. That way it isn't visible to the designer, which therefore won't mess with your layout. – Ben Voigt Jul 12 '11 at 03:37
  • I think you have me pointed in the right direction. I can probably set a constant for what DPI my Sizes were designed in to work correctly and compare that to `AutoScaleDimensions` before `PerformAutoScale` gets called to automatically adjust my sizes for different developer screens. Unless there's an easy way to tell VS to change my sizes at design time. Either way, my main problem definitely was thinking of the problem backwards. Thanks. – Kevin Stricker Jul 12 '11 at 04:10
  • @mootinator: You're welcome. You might also check the Addins/Extensions Gallery to see if anyone's made a VS plugin to deal with this, but at least looping through all your controls at runtime should be fairly straightforward. – Ben Voigt Jul 12 '11 at 04:25
1

Since I ran into the same issue recently, I wanna share my workaround.

First: KEEP AutoScaleMode = Font! This will work nicely at runtime to make your application DPI-aware. (Even cool per-Monitor DPI Aware).

However, the WinForms Designer in Visual Studio actually evaluates the fonts sizes based on the system's DPI, and thus rescales whenever it thinks it is necessary. This is the core of the problem. This evaluation is triggered because the fonts usually specify their size in "point" (to give a DPI-independent user experience).

My workaround is to change this for design time:

  • Set in your Forms Designer: Font = MS Sans; 11px. This is basically the OS default font, but specified with pixel-size. So no design-time rescaling will ever take place.
  • Now, to get High-DPI adaption back at runtime, change the Font back to use sizes specified in point. E.g. Font = SystemFonts.Default. A good place is in the Ctor right after InitializeComponents.

I wrote the details about my workaround on my Blog: http://www.sgrottel.de/?p=1581&lang=en

Knowleech
  • 1,013
  • 2
  • 9
  • 15