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

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

问题描述

我有一个Windows窗体,由3个文件组成:




  • AbcFrm.cs / p>


  • AbcFrm.Designer.cs


  • AbcFrm.resx




每当我对datagridview的任何属性进行一些更改或添加新事件时,控件的大小会在右侧和底部增大,并导致控件的某些部分超出界限。



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




  • 某些控件' .Size 值更改

  • 某些控件'。位置值更改

  • 控制' .Padding 值更改

  • 表单的 .AutoScaleDimensions / li>
  • 表单的 .ClientSize 变大



编辑



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



原因是代码可能已经在电脑中编辑具有不同的分辨率。但是,解决方案并不令人满意,因为它不能解决问题,只能确定原因。



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

解决方案

推理



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

我们假设有两台机器A和B.机器A有96 DPI和比例因子100%。机器B具有144 DPI和150%的比例因子作为其推荐设置。在机器A中创建了WinForm,机器B正在尝试编辑WinForm。



当机器B中的Visual Studio遇到机器B的DPI与用于创建WinForm,它会自动缩放WinForm中的对象,以使WinForm在机器B中显示为机器A的外观。因此,这将导致 WinForm.Designer.cs 文件中的一些属性将自动更改



解决方法



为了在高DPI机器中继续编辑WinForms而不进行任何自动对齐,我们需要将DPI设置在高DPI机器中,以匹配DPI用于创建WinForm的机器。这可以通过将缩放因子调整为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天全站免登陆