[英]Why is my form being resized when it is displayed?
I have a form that appears as a modal dialog. 我有一个显示为模态对话框的表单。 The form looks like this in the designer:
表单中的表单如下所示:
(width = 360, height = 215)
(宽度= 360,高度= 215)
When it is shown in the application, it gets 10 pixels taller and wider than is defined, leaving a wide margin around the bottom and left edges: 当它在应用程序中显示时,它比定义的更高和更宽10像素,在左下边缘留下宽的边缘:
(width = 370, height = 225)
(宽= 370,身高= 225)
The form is explicitly set to be 360x215 pixels in dimension, has a border style of FixedDialog
, inherits from System.Windows.Forms.Form
, and has no code in it to manipulate the dimensions (with the exception of the auto-generated designer file). 表单在维度上显式设置为360x215像素,具有
FixedDialog
的边框样式,继承自System.Windows.Forms.Form
,并且没有代码来操作维度(自动生成的设计器文件除外) )。 If I change the border style to FixedSingle
or FixedToolWindow
it appears the correct size (but I want it styled as FixedDialog
). 如果我将边框样式更改为
FixedSingle
或FixedToolWindow
它会显示正确的大小(但我希望它的样式为FixedDialog
)。
Any idea what is causing this? 知道是什么导致了这个吗?
I've fixed this by removing the MinimumSize
setting on the form. 我已经通过删除
MinimumSize
上的MinimumSize
设置来修复此问题。 It appears that if it is set to the same size (or near, but I haven't quite found the threshold yet) as the Size
property, the margins are introduced. 看来,如果将其设置为相同大小(或接近,但我还没有找到阈值)作为
Size
属性,则会引入边距。 As the form is not resizable, I don't need the MinimumSize
set so it can be removed. 由于表单不可调整大小,我不需要设置
MinimumSize
因此可以删除它。
I still don't understand why this is the case though. 我仍然不明白为什么会这样。
First, your form seems to have AutoScaleMode
set to Font
. 首先,您的表单似乎将
AutoScaleMode
设置为Font
。 This causes a form resize depending on the used font. 这会导致表单调整大小,具体取决于使用的字体。
Second, ensure to have the following lines before creating the main form: 其次,确保在创建主窗体之前有以下几行:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); // Not using this - or using true - will cause a different font rendering
...
Application.Run(new Form1()); // this creates your main form
(Usually this is part of the static Main
method in Program.cs
) (通常这是
Program.cs
静态Main
方法的一部分)
Not using those lines causes the usage of a different font rendering (have a look at your screenshots - the letters don't look exacly identical!) 不使用这些行导致使用不同的字体渲染(看看你的截图 - 字母看起来不太相同!)
I had similar problem with my form. 我的表格有类似的问题。 Finally I found out that the problem was with the Maximum and Minimum Size of the form in properties.
最后我发现问题在于属性中表单的最大和最小大小。 If you want a fixed, unresizable form, you have to disable them.
如果您想要一个固定的,不可调整的表单,则必须禁用它们。
Hope this helps. 希望这可以帮助。
I have created one form, changed the font and set the FormBorderStyle
to FixedDialog
, but I did not face any issue. 我创建了一个表单,更改了字体并将
FormBorderStyle
设置为FixedDialog
,但我没有遇到任何问题。 Just have a check that what exactly are you setting on form load. 只需检查一下您在表单加载上的确切设置。
// this needs to be set as joe said is correct.
Application.SetCompatibleTextRenderingDefault(false);
If you can provide the code which you writing on formLoad method. 如果您可以提供您在formLoad方法上编写的代码。 So that I can try to rectify the problem.
这样我就可以尝试纠正这个问题。
Why don't you brute force the issue with the code: 你为什么不用代码强行解决问题:
protected override void SetClientSizeCore(int x, int y)
{
base.SetClientSizeCore(360, 215);
}
which sets the client area. 设置客户区域。 You need to calculate what values you want.
您需要计算所需的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.