C# Winforms | 窗体边框粗细

5

有关常规表单边框有多厚的文档吗?

目标:
我创建了一个宽度为800px的用户控件。我想要在全分辨率 (800px - 所有可见) 的情况下,使用新实例引发一个弹出 (通常是普通窗体)。

问题: 将窗体设置为Form.Size.Width = 800无法完成任务。这看起来像窗体的边框已经包含在窗体的宽度属性中了。我需要减去边框的宽度。

应该是: 2px + 800px + 2px

如果您想看一些代码,请告诉我,但我认为在这里不必要。

编辑:

enter image description here

弹出控件后:

enter image description here

弹出窗口代码:

private void buttonPopup_Click(object sender, EventArgs e)
{
    Form MyPopup = new Form();
    customControl MyUserControl = new customControl();

    MyUserControl.Dock = DockStyle.Fill;

    Rectangle rc = MyUserControl.RectangleToScreen(MyUserControl.ClientRectangle);

    //int thickness = SystemInformation.Border3DSize.Width;
    //MyPopup.MaximumSize = new Size(MyUserControl.Size.Width + (thickness*2), 1500);

    MyPopup.Controls.Add(MyUserControl);
    MyPopup.MaximumSize = new Size(rc.Width, rc.Height);
    MyPopup.Show();
}

我觉得你的代码看起来很合理。但是结果还是一样的。 userControl 显示的略小一些。我知道我在不专业地放置按钮时使用了 dock = fill。但是除此之外,必须有一个解决方案来 设置正确的大小


请查看 SystemInformation 类和 ClientRectangle 属性。 - Dmitry Bychenko
似乎您需要设置 ClientSize(窗体大小不包括边框、标题等)。使用 MyPopUp.ClientSize = new Size(...); 而不是 MaximumSize - Dmitry Bychenko
1个回答

6
似乎您在寻找的是:
int thickness = SystemInformation.Border3DSize;

另一种(在我看来更好的)可能性是使用控件的ClientRectangle。例如:

// Client rectangle in screen coordinates
Rectangle rc = MyControl.RectangleToScreen(MyControl.ClientRectangle);

// Let's align context menu (its width) to bottom of the control
MyContextMenuStrip.AutoSize = false;
// Depending on actual dropdown control you may want align either via
//   Width = rc.Width;
// Or 
//   ClientSize = new Size(rc.Width, someHeight);
MyContextMenuStrip.Width = rc.Width;

// Let's show context menu at the bottom of the control
MyContextMenuStrip.Show(new Point(rc.Left, rc.Bottom));

关于厚度:调试显示Border3DSizewidthheight为2像素。我猜这些值有点太小了。我会检查一下ClientRectangle - C4d
1
@C4ud3x:是的,ClientRectangle(特别是对于用户定义的控件)是一种更好的方式:你的控件可以是,比如说,平面等。 - Dmitry Bychenko
在这种情况下,rc.Widthrc.Height可以为您服务。 - Dmitry Bychenko
2
似乎您需要设置 ClientSize(控件/窗体的大小,不包括边框标题滚动条等)。使用 MyPopUp.ClientSize = new Size(...);而不是 MaximumSize - Dmitry Bychenko
让我们在聊天中继续这个讨论 - Dmitry Bychenko
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接