Winforms draw border and move when FormBorderStyle is set to None

I show winform as a dialog (with ShowDialog above the main window). Thus, I set FormBorderStyle to None, because I do not need either control boxes or a title bar. Although, I would like the drawn border (for example, a blue border, like ordinary windows), and retained the ability to move the form. I do not need the ability to resize it. I tried to draw a border by overriding OnPaint, but it is never called. Here is my code:

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); } 

Any help would be greatly appreciated.

+2
c # winforms border move
Oct 25 '10 at 9:20
source share
3 answers

The Paint method is incorrect here, because it does not create the so-called non-client area of ​​the form, for example. border and title bar.

To hide the title bar, you need to set the ControlBox property to false and clear the Text form property. Set the border to FixedDialog to make the form impossible.

To maintain the ability to move the form without a title, you need to override WndProc .

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

This is basically a standard way to process a WM_NCHITTEST message and cheat saying: the mouse cursor is in the window title [return value 0x2] so that you can move your form even if you click in the client area and drag it.

+2
Oct 25 2018-10-10T00:
source share

My problem was to have a resizable shape with a thin border.

I set the FormBorderStyle parameter to None

I use the dock panel, which contains all my controls.

I use padding to set the width of the borders.

And then:

 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; } 
+1
Jan 13 '12 at 9:36
source share

Since more information does not seem available, I will leave the border, as suggested, set the FixedDialog value with the ControlBox property to false, and the Text form is cleared. I would prefer another border color and the ability to move the window. Anyway, thanks for the answers.

0
Oct 26 2018-10-26
source share



All Articles