FormBorderStyle 为 NONE 时的表格高度问题 [英] Form height problem when FormBorderStyle is NONE

查看:25
本文介绍了FormBorderStyle 为 NONE 时的表格高度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无边框表单(FormBorderStyle = None),高度为 23 像素(在设计器中设置)

I have a borderless form (FormBorderStyle = None) with the height of 23 pixels (set in the designer)

当 .NET 在运行时绘制我的表单时 - 它绘制 38 像素高(它出于某种原因添加了标题栏的高度).

When .NET draws my form at runtime - it draws it 38 pixels high (it adds the height of a title-bar for some reason).

MessageBox.Show(this.Height.ToString()); //this shows 38!! why?

要解决这个问题,我必须设置Height = 23;"在 Form_Load 事件中.

To work it around I have to set "Height = 23;" in the Form_Load event.

private void MyForm_Load(object sender, EventArgs e)
{
    this.Height = 23; //workaround. wtf??
}

您可以在 Visual Studio 2010(Winforms 应用程序,目标框架 - 2.0)中自行尝试.

You can try this yourself in Visual Studio 2010 (Winforms App, target Framework - 2.0).

什么?

推荐答案

是的,这是一个错误,各种各样.请注意在设计器中如何使用 Width 和 Height 属性设置窗体的大小.这些属性包括边框和标题栏的大小.但是,这是一个问题,您的表单可能会在用户增加了标题栏字体大小的机器上运行.这将减少窗口客户区的大小.或者换句话说,表单的 ClientSize 属性会在该机器上更改.为控件留出更少的空间,并且非常严重地弄乱了表单的设计.

Yeah, it is a bug, of sorts. Note how in the designer you set the size of the form with the Width and Height properties. Those properties include the size of the borders and the title bar. That's a problem however, your form may run on a machine where the user has increased, say, the title bar font size. That then would reduce the size of the window's client area. Or in other words, the form's ClientSize property would change on that machine. Leaving less room for the controls and messing up the design of your form pretty badly.

Form 类中的代码在 创建 Handle 之后运行,就在 Load 事件运行之前.它使用您机器上的相同 ClientSize 重新计算表单的大小.现在一切都好了,表单的高度将与您在设计器中设置的高度不匹配,但表单看起来相同,控件的布局也相同.

There's code inside the Form class that runs after the Handle is created, right before the Load event runs. It recalculates the Size of the form, using the same ClientSize you had on your machine. Now everything is good, the Height of the form won't match the one you set in the designer but the form otherwise looks the same and the layout of the controls is identical.

同样的代码还确保窗口不会变得太小.这就是它失败的地方,它没有对 FormBorderStyle 属性给予足够的关注.如您所见,将高度裁剪为标题栏大小加上客户区高度.它还可以防止表单变得太窄,以确保图标和最小/最大/关闭按钮始终可见.即使你没有.

That same code also ensures that the window doesn't get too small. And that's where it falls over, it doesn't pay enough attention to the FormBorderStyle property. Clipping the height to the title bar size plus the client area height, as you found out. It also prevents the form getting too narrow, trying to make sure that the icon and min/max/close buttons are always visible. Even if you don't have any.

解决方法是在此代码运行后更改 ClientSize,OnLoad 覆盖或 Load 事件处理程序是正确的位置.请注意,如果您像这样对表单大小进行硬编码,那么您还应该将 AutoScaleMode 属性设置为 None.确保这不会在具有不同 DPI 设置的机器上造成问题.

The workaround is to change the ClientSize after this code runs, the OnLoad override or Load event handler is the right place for that. Beware that if you hard-code the form size like this then you should also set the AutoScaleMode property to None. Make sure that this doesn't cause trouble on a machine that has a different DPI setting.

这篇关于FormBorderStyle 为 NONE 时的表格高度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆