为什么 Visual Studio 会自动更改表单的布局? [英] Why does Visual Studio automatically changes the layout of my form?

查看:39
本文介绍了为什么 Visual Studio 会自动更改表单的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3 个文件的 Windows 窗体:

  • AbcFrm.cs

  • AbcFrm.Designer.cs

  • AbcFrm.resx

每当我对 datagridview 的任何属性做一点更改或添加新事件时,控件的大小都会向右侧和底部变大,并导致控件的某些部分越界.

我比较了 .Designer.cs 文件之前和之后的细微属性更改(例如,将 datagridview 的 tabStop 属性值从 true 更改为 false),并发现在 .Designer 中自动添加/更改了一些行/属性..cs 文件:

  • 一些控件的.Size值变化
  • 某些控件的 .Location 值更改
  • 某些控件的 .Padding 值更改
  • 表单的 .AutoScaleDimensions 变大
  • 表单的 .ClientSize 变大

编辑

我意识到当我将分辨率从 150% 更改为 125% 并重新打开应用程序时,由于 Windows 窗体现在具有更大的缩放尺寸,控件又回到了正确的位置.我的问题是类似的 这个问题

原因是代码可能是在不同分辨率的计算机上编辑过的.然而,解决方案并不令人满意,因为它没有解决问题,只是找出原因.

如何防止这种情况发生?我使用的是 Visual Studio 2010,我的操作系统是 Windows 10.

解决方案

推理

这是由于 AutoScaleMode 属性,它根据不同开发机器之间的每英寸点数 (DPI) 的差异来缩放 Winform 中对象的大小和位置.这是一项不能也不应该改变.有关官方文档,请参阅 MSDN 上的Windows 窗体中的自动缩放.>

假设有两台机器,机器 A 和 B.机器 A 的 DPI 为 96,缩放因子为 100%.机器 B 的推荐设置为 144 DPI 和 150% 缩放因子.在机器 A 中创建了一个 WinForm,而机器 B 正在尝试编辑该 WinForm.

当机器 B 中的 Visual Studio 遇到机器 B 的 DPI 与用于创建 WinForm 的 DPI 不同时,它会自动缩放 WinForm 中的对象,以尝试使 WinForm 在机器 B 中显示为它在机器中的样子一个.因此,这会导致 WinForm.Designer.cs 文件中的某些属性自动更改.

解决方法

为了在没有任何自动对齐的情况下继续在高 DPI 机器中编辑 WinForms,我们需要在高 DPI 机器中设置 DPI 以匹配用于创建 WinForm 的机器中的 DPI.这可以通过将缩放因子调整为 100%(96 DPI;右键单击桌面 > 显示设置)并重新启动 Visual Studio(或重新登录到 Windows,如果不起作用)来完成.

注意:这里的关键是将 DPI 调整为用于创建 WinForm 的机器的原始 DPI.仅调整比例因子可能不起作用.您可能需要调整屏幕分辨率.

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.

解决方案

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.

这篇关于为什么 Visual Studio 会自动更改表单的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆