Search code examples
winformsvisual-studio-2017dpi

Why is Visual Studio 2017's new AutoScale support resizing my Forms app at runtime?


We have a Windows Forms application used for testing our products that has had pretty much the same forms and dialogs for nearly 10 years now. AutoScaleMode in the designer is set to Font for both forms, and that hasn't changed since the original design. AutoSize is set to false and just for good measure (I guess) AutoSizeMode is set to GrowOnly. The following source lines are in Program.cs:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Remember, this has been working flawlessly for years...

One of the recent updates to VS2017 included AutoScale support for monitors with different DPI. I have a relatively high DPI monitor, so when I made some changes to a Settings dialog (adding controls, etc) I started getting bright yellow banners cross the top of the designer surface telling me that AutoScaling was set to 125%, and would I like to change that? I tried going back and forth, and when in 100% mode (Autoscaling off) I was warned that XAML forms might not display correctly. Fine, I am working with Forms so I went back to "normal" scaling and the form looked fine.

Until I tried running the program. Now when I start the program the main form looks like this (details deleted to protect the guilty):

enter image description here

...but when I open the settings dialog it looks like this:

enter image description here

Yikes! It looks worse in practice, the relative images here don't do justice to the difference in size and scale.

I have no idea what got changed or how / why this is happening, but there's no way I can put this into production. I've tried changing the AutoScale settings, to no avail. Can somebody point me in the right direction here?

Thanks in advance!

EDIT: It seems the rescaling only happens on my machine when I run the app in Debug. Whatever is messing up the display of the forms on my machine doesn't do that on my associates' machines and isn't replicated to the executable produced by the build server.

On another note, I tried every DPI-related setting I could find, added those that weren't there due to the program's age, all to no avail. Nothing I have tried has had any effect on the program's display weirdness on my machine. Ugh.


Solution

  • I tried all the tricks I could find, short of disabling AutoScale completely, and nothing worked. I finally merged our develop branch in to my feature branch and inspected all 270 edits, choosing the oldest settings I could find for all size parameters, all controls. Ugh. Along the way I stumbled across the following line in the Designer.cs file for the form:

    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    

    This setting is NOT accessible from Visual Studio's designer, as far as I can tell, nor can I find anything about how or why it gets set. What I did find was that in the earlier version that did work as expected, the values were (8F, 17F) -- so I manually edited the line to match the older, working version. Success!

    I also checked my Windows display settings, and the Custom Scaling value was at 100%, so I used the registry hack mentioned in one of the articles I found following the first link from the comment above (thanks, @Jimi) to disable auto-scaling in Visual Studio, then turned off the notification. Now I'm (finally) back in business.

    Registry hack is here: https://learn.microsoft.com/en-us/dotnet/framework/winforms/disable-dpi-awareness-visual-studio