Why does Visual Studio automatically changes the l

2019-01-20 01:01发布

I have a Windows form which consists of 3 files:

  • AbcFrm.cs

  • AbcFrm.Designer.cs

  • AbcFrm.resx

Whenever I do a little change to any properties of datagridview or add new events, the size of the controls grows larger to the right and bottom side and causes some part of the controls to go out of bounds.

I have compared the .Designer.cs file before and after I perform a minor property change (e.g. change a datagridview's tabStop property value from true to false) and discovered some lines/properties were added/changed automatically in the .Designer.cs file :

  • some controls' .Size value changes
  • some controls' .Location value changes
  • some controls' .Padding value changes
  • the form's .AutoScaleDimensions becomes larger
  • the form's .ClientSize becomes larger

Edit

I realize when I change the resolution from 150% to 125% and reopen the application, the controls are back in the correct place due to the windows form is now having a larger scaling size. My problem is similar to this question

The cause is that the code might have been edited in a computer with different resolution. However, the solution is not satisfying as it does not solves the problem, only identifying the cause.

How do I prevent this from happening? I'm using Visual Studio 2010 and my OS is Windows 10.

4条回答
男人必须洒脱
2楼-- · 2019-01-20 01:41

The issue propably lies in Visual Studio 2010 or in the Windows 10 (depends how you look at it). Graphics API's were changed after Windows 7 for which VS 2010 was made. Then there was normal to have 100% DPI scalling and nothing else. Maybe you should try installing Visual Studio 2015 Community Edition which should fix the issue.

EDIT:

Currently there is no way to lock WinForms editor from changing the settings in the designer file. This is actually expected behaviour so the positioning/size/scaling values change to adjust the current display settings so the values can be adjusted if Autoscaling and DPI awarness are turned on. Here are some good resources for working with High DPI settings:

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-20 01:50

My solution to this was to add the registry key LogPixels and rebooting the machine. This key was previously not present on my machine.

[HKEY_CURRENT_USER\Control Panel\Desktop] "LogPixels"=dword:00000096 

I am on Windows 10 with a high resolution display. I tried to toggle my display settings back to 100% but it had no effect.

This registry setting worked for me.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-20 01:59

Having run into this problem myself recently I've spent some time following these responses and other resources elsewhere as well. I encountered this issue while developing an Excel Addin using the VSTO tools in VS 2015 Pro, under Windows 10.

After playing around quite a bit, I then created a one-form Windows Forms solution with which I could observe the effect of various things such as the AutoScale properties, etc. I've run into something which I think is truly strange:

Within the Excel Addin solution, I set a breakpoint at the first line of code in the form.Load handler. At that point, I see:

Screen.PrimaryScreen.Bounds   {X=0 Y=0 Width=5760 Height=3240}

This is interesting well beyond the screen resolution on my system, which is 3840x2160.

I close this solution and immediately open the single-form "test" solution, which has one form (windows form) which has only 2 labels which are filled in the Load event with Top/Left/Width/Height values and a button whose title does the same, for the button itself. No other code whatsoever. Within this solution, run on the same system, I see:

Screen.PrimaryScreen.Bounds   {X=0 Y=0 Width=1920 Height=1080}

Why would Windows / Visual Studio report different screen size, on the same machine, with absolutely no changes made to system settings of any sort?

Basic system settings:

Lenovo Yoga 920, Screen resolution = 3840 x 2160, Scaling = 200%

Again, no changes made at all between running the two different solutions within Visual Studio.

ANY hints or ideas will be most welcome!

On the whole, it seems to me that MS has really stuffed things up in terms of being able to use VS to create portable solutions! Just my 2c....

查看更多
Evening l夕情丶
5楼-- · 2019-01-20 02:03

Reasoning

This was due to AutoScaleMode property which scales the size and position of objects in winforms based on the difference in Dots per inch (DPI) between different development machines. This is a feature that cannot and should not be changed. For official documentation, see Automatic Scaling in Windows Forms on MSDN.

Let's say there are two machines, machine A and B. Machine A has 96 DPI and scaling factor 100%. Machine B has 144 DPI and 150% scaling factor as its recommended settings. A WinForm was created in machine A and machine B is trying to edit that WinForm.

When Visual Studio in machine B encounters that machine B's DPI is different than the one used to create the WinForm, it automatically scales the objects in the WinForm to try to make the WinForm appear in machine B as how it would look in machine A​​. Hence, this causes some properties in WinForm.Designer.cs file to be automatically changed.

Workaround

In order to continue editing WinForms in high DPI machines without any automatic alignments, we need to set the DPI in high DPI machines to match the DPI in the machines used to create the WinForm. This can be done by adjusting the scaling factor to 100% (96 DPI; right-click on desktop > Display settings) and restarting Visual Studio (or re-login to Windows, if that does not work).

NOTE: The key here is to adjust the DPI to the original DPI of the machines used to create the WinForm. Just adjusting the scaling factor might not work. You might need to play around with the screen resolution.

查看更多
登录 后发表回答