问题
I am developing a C# WinForms app for .NET 4.0 in VS 2017 on my ASUS NV552 laptop with UltraHD monitor with 282ppi. To work comfortably, I set the system scale factor to 300% in my Windows 10 Pro Creators Update. When I compile and launch the project, the size of the form differs from what I see in the VS Designer and it seems, .NET stops recalculating the form size at all for big form sizes.
For instance, when the form's Size
property is set to 1606; 1284
in the VS property browser, the result size of the form on the screen displayed from the exe is 421x285. However, when I resize the form in the Designer say to 1943x1284, I see the same form of 421x285 in the exe!
Moreover, it seems, for smaller sizes I even see the result form with improper proportions of width and height.
If it helps somehow, below are the corresponding auto-scale settings for this form in the .designer.cs file when Size
equals 1606; 1284
:
this.AutoScaleDimensions = new System.Drawing.SizeF(19F, 37F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1570, 1181);
I do understand that by default VS 2017 produces DPI-unaware exe applications, and that's ok if the form size is reduced in pixels when it is displayed from the exe (as if it were developed for 96ppi). Microsoft solved this problem with the release of .NET Framework 4.7 - now we can make our WinForms forms DPI-aware using the following setting in app.config (according to High DPI support in Windows Forms):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<System.Windows.Forms.ApplicationConfigurationSection>
<add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>
</configuration>
But I need to solve the problem for old legacy DPI-unaware WinForms apps. Is there a solution?
Please, don't write useless comments containing phrases like "WinForms is dead" and "switch to WPF". Write only if you have ideas how to improve the described behavior in WinForms projects.
回答1:
WinForms Designer does not work well when HDPI scaling is enabled. You can somewhat improve your experience is you declare VisualStudio process as DPI-unaware, as described in this blog post: https://code4ward.net/2016/11/29/visual-studio-winforms-designer-on-highdpi/
Unfortunately there is no easy way to convert an existing application to support DPI awareness, each form has to be worked on individually.
Thank you, Tanya
来源:https://stackoverflow.com/questions/44898945/different-and-wrong-size-of-a-winforms-form-on-high-resolution-screen-in-vs-ide