简体   繁体   English

为什么我的表单在显示时会调整大小?

[英]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: 表单中的表单如下所示:

在Visual Studio的设计视图中 (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 ). 如果我将边框样式更改为FixedSingleFixedToolWindow它会显示正确的大小(但我希望它的样式为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.

相关问题 我在窗体上有一个DataGridView,但在运行窗体时却没有显示。 为什么? - I have a DataGridView on my form that is not being displayed when I run my form. Why? 为什么在显示或加载事件时我的表单大小无法正确调整? - Why isn't my form resized correctly when on shown or load event? 为什么显示的图像更大? - Why is my image being displayed larger? 调整表单大小时,为什么线条没有穿过图像的同一部分? - Why do the lines not cross the same part of the image when the form is resized? 调整表单大小时调整组件大小 - Resize components when the form is resized 调整大小时,某些图像正在旋转 - Some images are being rotated when resized 当我运行 Windows 窗体应用程序时,为什么我的 if 语句和 MessageBox 没有被点击或显示? - Why aren't my if statements and MessageBox's not getting hit or displayed when I run my Windows form applications? 表单未在C#中显示 - Form not being displayed in c# 为什么我的表单控件没有一个被识别为ComboBoxes? - Why are none of my form's controls being recognized as being ComboBoxes? 调整TextBox的大小,以便在调整控件大小时正确显示字体 - Resizing a TextBox so font is correctly displayed when Control is resized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM