简体   繁体   English

Windows 窗体大小不匹配

[英]Windows Forms sizes not matching

I've got some very strict requirements on windows form sizes for elements and I've been having some trouble actually matching that up, I figured out the problem but I don't understand why I'm getting it.我对元素的 Windows 窗体大小有一些非常严格的要求,但在实际匹配时遇到了一些麻烦,我发现了问题,但我不明白为什么我会得到它。 I have a picture box that needs to be a specific size, when I set it to that size in the properties panel of visual studio, it doesn't actually take that size, but a value smaller than that.我有一个需要特定大小的图片框,当我在 Visual Studio 的属性面板中将其设置为该大小时,它实际上并没有采用该大小,而是一个小于该大小的值。 It was throwing off my development.它抛弃了我的发展。 I can rectify it by manually setting the size value in the form code, however I'd like to know why the properties tab doesn't automatically do it correctly.我可以通过在表单代码中手动设置大小值来纠正它,但是我想知道为什么属性选项卡不会自动正确执行。 I've just got it outputting the precise size value to a message box.我刚刚将精确的大小值输出到消息框。

真实大小

属性窗口

I have noticed the exact same behaviour in my project today.我今天在我的项目中注意到了完全相同的行为。 The form and all objects on it hat a certain size, but when run, the size was all different.窗体及其上的所有对象都有一定的大小,但是运行时,大小都不同。

After checking the InitializeComponents() I found this line检查 InitializeComponents() 后,我发现了这一行

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

so I checked the according property, and I changed it to "None".所以我检查了相应的属性,并将其更改为“无”。 Have never actually witnessed this behaviour before, but this did the trick for me.以前从未真正目睹过这种行为,但这对我有用。 Now the Forms and everything else has the specified size.现在表单和其他所有内容都具有指定的大小。

The Size is actually size of whole window including the borders of windows form.大小实际上是整个窗口的大小,包括窗体的边框。 You can see that the inner portion has exactly 18 pixels less for both width and height.您可以看到内部部分的宽度和高度都少了 18 个像素。 You might calculate the desired width and height to assign to the form.您可以计算要分配给表单的所需宽度和高度。 Like if you want 100 x 100 pixel inner window size, you can assign 118 x 118 pixel for the size.就像如果你想要 100 x 100 像素的内部窗口大小,你可以为大小分配 118 x 118 像素。

Any of the following properties of PictureBox could have caused it: PictureBox 的以下任何属性都可能导致它:

  • Anchor
  • Dock码头
  • Margin利润
  • MinimumSize最小尺寸
  • MaximumSize最大尺寸
  • SizeMode大小模式

Also need to check the direct parent (container) of your PictureBox .还需要检查您的PictureBox的直接父级(容器)。 Is it directly put in the Form, or inside another container like GroupBox or Panel ?它是直接放在 Form 中,还是放在另一个容器(如GroupBoxPanel If latter is the case, you should check properties of the parent object too.如果是后者,您也应该检查父对象的属性。

Size of you pictureBox is determined according to size of Form.你的pictureBox的大小是根据Form的大小决定的。 That is why you picturebox does not fit size you defined.这就是为什么您的图片框不适合您定义的大小。

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

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