简体   繁体   English

复制和粘贴UserControl会导致调整大小

[英]Copy and Pasting UserControl causes resize

I'm developing in C#, and I'm creating a framework with lots of user controls. 我正在用C#开发,并且正在创建一个包含许多用户控件的框架。 The problem I've got is that every UserControl I have is being resized (enlarged) when I copy and paste it within the designer. 我遇到的问题是,当我将其复制并粘贴到设计器中时,我拥有的每个UserControl都会被调整大小(放大)。 It is a very simple scenario to reproduce (no code samples necessary, as this is reproducible from a blank project with no code added): 复制是一个非常简单的场景(不需要代码示例,因为这可以从没有添加代码的空白项目中复制):

  • Create a new project. 创建一个新项目。

  • Create a new UserControl1. 创建一个新的UserControl1。 Set the BorderStyle = FixedSingle (this is just to make things easier to see). 设置BorderStyle = FixedSingle(这只是为了使事情更容易看到)。

  • Create a new form. 创建一个新表格。 Set the Font on the form to "Tahoma 9pt." 将窗体上的字体设置为“ Tahoma 9pt”。 Since this is slightly larger than the default font (Ms Sans Serif 8pt), it will cause a small amount of scaling, and that is expected (and desired). 由于它比默认字体(Ms Sans Serif 8pt)稍大,因此将导致少量缩放,这是预期的(和期望的)。

  • Drag an instance of the UserControl1 onto the form. 将UserControl1的实例拖到窗体上。

  • Copy and paste the instance of the UserControl1. 复制并粘贴UserControl1的实例。

The copy will be scaled again, making it larger than the original. 副本将再次缩放,使其比原始副本更大。 Try it, the problem will be obvious to you. 尝试一下,问题对您来说很明显。

I sort of know why it is happening, but I need to find a way around it, because I can't afford to be re-sizing every control every time I copy and paste it. 我有点知道为什么会发生这种情况,但是我需要找到解决方法,因为每次复制和粘贴控件时,我都无法调整每个控件的大小。 I know that I can get around this by changing the UserControl's "AutoScaleMode" property to "None," but the problem is that I want the control to be rescaled if the user has their system font-scaling set to greater than 100%. 我知道我可以通过将UserControl的“ AutoScaleMode”属性更改为“ None”来解决此问题,但是问题是,如果用户将系统字体缩放比例设置为大于100%,则希望重新缩放控件。 Setting AutoScaleMode to Font provides me with the proper re-sizing in those circumstances, but gives me the unfortunate re-scaling during a copy and paste operation within the designer itself. 在这些情况下,将AutoScaleMode设置为Font可以为我提供适当的大小调整,但是不幸的是,在设计器内部进行复制和粘贴操作期间,我无法进行重新缩放。

Obviously the designer has a way of dealing with this, because built-in controls don't suffer from this problem (try the same experiment with TextBox, and you will see it doesn't get rescaled on a copy and paste). 显然,设计人员可以解决此问题,因为内置控件不会遇到此问题(使用TextBox进行相同的实验,您会发现它不会在复制和粘贴时重新缩放)。

Anyone have a good approach for dealing with this sort of thing? 任何人都有处理这种事情的好方法吗?

Edit: Had originally said "AutoSizeMode" when I really meant "AutoScaleMode." 编辑:当我真正的意思是“ AutoScaleMode”时,最初说的是“ AutoSizeMode”。 I've corrected this in the the original text. 我已经在原始文本中对此进行了更正。

When designing your user control (before it is put on the Form), set the AutoScaleMode of the user control to "Inherit". 在设计用户控件时(在将其放在窗体上之前),请将用户控件的AutoScaleMode设置为“ Inherit”。 This will give the user control the same scaling as you set on the Form. 这将使用户控件具有与在窗体上设置的相同的缩放比例。

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

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