简体   繁体   English

当显示设置更改时,Windows窗体控件的大小更改

[英]Windows forms controls' sizes changes when display settings change

I'm working on a Windows Forms project. 我正在开发一个Windows Forms项目。 In this project the controls' sizes are set in the designer and autosize is turned off. 在此项目中,控件的大小在设计器中设置,并且自动调整大小已关闭。 Yet when the display settings in Windows 7 are set to medium (125%) or large (150%) , the controls change size. 但是,当Windows 7中的显示设置设置为中(125%)大(150%)时 ,控件会更改大小。 How can I prevent this behavior? 我该如何防止这种行为?

Most likely you could turn off this behaviour by setting your form's AutoScaleMode property to AutoScaleMode.None . 最有可能的,你可以通过表单的设置关闭此行为AutoScaleMode属性AutoScaleMode.None

That being said, don't just turn auto-scaling off, because it's most likely a good thing! 话虽这么说, 不要只是关闭自动缩放,因为它很可能是一件好事! It makes your forms work at various resolutions and font scale settings. 它使您的表单以各种分辨率和字体比例设置工作。 For example, if you design your forms but forget about users with impaired vision (eg people who increase the system font scaling to unusually high percentages), chances are that your form might still work, thanks to auto-scaling. 例如,如果您设计表单但忘记了视力受损的用户(例如,将系统字体缩放比例增加到异常高的百分比的人),由于自动缩放,您的表单可能仍然有效。

As the other answers suggest, instead of specifying the positions and sizes of each control in terms of fixed pixel values and then turning off auto-scaling, use automatic layout techniques: 正如其他答案所示, 不是根据固定像素值指定每个控件的位置和大小,然后关闭自动缩放, 而是使用自动布局技术:

  • putting controls inside the various container elements for arranging controls (in a grid – TableLayoutPanel , one after the other — FlowLayoutPanel , etc.); 将控件放在各种容器元素中以安排控件(在网格中 - TableLayoutPanel ,一个接一个FlowLayoutPanel等);
  • setting the Dock and Anchor properties of each control; 设置每个控件的DockAnchor属性;
  • setting the AutoSize and AutoSizeMode properties of each control. 设置每个控件的AutoSizeAutoSizeMode属性。

Doing this correctly takes some practice, but makes your forms' layout much more robust. 正确地执行此操作需要一些练习,但会使表单的布局更加健壮。

使用控件的Anchor和Dock属性。

在UserControl或Form上将AutoScaleMode设置为Dpi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM