winforms绘制边框并在FormBorderStyle设置为None时移动

我将winform显示为对话框(在主窗口上显示ShowDialog)。 所以,我将FormBorderStyle设置为None,因为我既不想要控件盒也不想要标题栏。 虽然,我想画一个边框(例如像普通窗户一样的蓝色边框)并保持移动表格的能力。 我不需要调整它的大小。 我试图通过覆盖OnPaint来绘制边框,但它永远不会被调用。 这是我的代码:

protected override void OnPaint (PaintEventArgs e) { base.OnPaint (e); int borderWidth = 2; Color borderColor = Color.Blue; ControlPaint.DrawBorder (e.Graphics, e.ClipRectangle, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid); } 

任何帮助将不胜感激。

这里的Paint方法是错误的,因为它不会绘制窗体的所谓非客户区域,例如边框和标题栏。

要隐藏标题栏,您需要将ControlBox属性设置为false并清除窗体的Text属性。 将边框设置为FixedDialog以使窗体不可调整。

要保留在没有标题栏的情况下移动表单的function,您需要覆盖WndProc

 protected override void WndProc(ref Message m) { switch (m.Msg) { case 0x84: m.Result = new IntPtr(0x2); return; } base.WndProc(ref m); } 

基本上这是处理WM_NCHITTEST消息和作弊的标准方法,说 – 鼠标光标在窗口的标题[返回值0x2]上,因此即使您单击客户区并拖动它,您也可以移动表单。

我的问题是要有一个带有薄边框的可resize的表单。

我将FormBorderStyle设置为None

我使用一个包含所有控件的停靠面板。

我使用面板填充来设置边框宽度。

然后 :

 Point ResizeLocation = Point.Empty; void panResize_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Left) { ResizeLocation = e.Location; ResizeLocation.Offset(-panResize.Width, -panResize.Height); if (!(ResizeLocation.X > -16 || ResizeLocation.Y > -16)) ResizeLocation = Point.Empty; } else ResizeLocation = Point.Empty; } void panResize_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Left && !ResizeLocation.IsEmpty) { if (panResize.Cursor == Cursors.SizeNWSE) Size = new Size(e.Location.X - ResizeLocation.X, e.Location.Y - ResizeLocation.Y); else if (panResize.Cursor == Cursors.SizeWE) Size = new Size(e.Location.X - ResizeLocation.X, Size.Height); else if (panResize.Cursor == Cursors.SizeNS) Size = new Size(Size.Width, e.Location.Y - ResizeLocation.Y); } else if (eX - panResize.Width > -16 && eY - panResize.Height > -16) panResize.Cursor = Cursors.SizeNWSE; else if (eX - panResize.Width > -16) panResize.Cursor = Cursors.SizeWE; else if (eY - panResize.Height > -16) panResize.Cursor = Cursors.SizeNS; else { panResize.Cursor = Cursors.Default; } } void panResize_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { ResizeLocation = Point.Empty; } 

由于没有更多信息可用,我将按照建议离开边框,设置为FixedDialog,并将ControlBox属性设置为false并清除表单的文本。 我更喜欢边框的另一种颜色和移动窗户的能力。 无论如何,非常感谢答案。