【问题标题】:C# Winforms | Form-border thicknessC# Winforms |表格边框厚度
【发布时间】:2015-11-13 18:28:46
【问题描述】:

是否有任何文档说明常规形式的边框有多厚?

目标:
我创建了一个宽度为 800px 的 userControl。我想用全分辨率(800px - 一切可见)的新实例弹出一个弹出窗口(通常是正常形式)。

我的问题: 将表单设置为Form.Size.Width = 800 不会这样做。看起来表单的边框包含在表单的宽度属性中。我需要减去那个边框。

我应该是这样的: 2px + 800px + 2px

如果你想看一些代码告诉我,但我认为这里没有必要。

编辑

弹出控件后:

弹出代码:

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属性
  • 看来你必须设置ClientSize(表单的大小不包括边框、标题等)。 MyPopUp.ClientSize = new Size(...); 而不是 MaximumSize

标签: c# winforms size border formborderstyle


【解决方案1】:

看来你在找

int thickness = SystemInformation.Border3DSize;

另一个(以及,INHO,一个更好)的可能性是使用控件的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));

【讨论】:

  • 对于粗细:调试显示widthheightBorder3DSize 有2 个像素。我想这个值有点太小了。生病检查ClientRectangle
  • @C4ud3x: 是的,ClientRectangle(尤其是用户定义控件)是一种更好的方式:你的控件可以是,比如说,平等
  • 只是为了保持相同的想法:我不在乎弹出窗口的位置。我只关心正确的大小,所以它适合我的 userControl。生病添加2个屏幕截图。那样更容​​易看到。
  • 在这种情况下,rc.Widthrc.Height 随时为您服务
  • 看来你必须设置ClientSize(控件/表单的大小不包括它的borderscaption滚动条等)。 MyPopUp.ClientSize = new Size(...);而不是MaximumSize
猜你喜欢
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 2015-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多