I display a winform as a dialog (with ShowDialog over a main window). So, I set the FormBorderStyle to None because I wanted neither the control boxes nor the title bar. Though, I would like a border drawn (for example a blue border like normal windows) and keep the ability to move the form. I don't 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 = 开发者_StackOverflow中文版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.
The Paint
method is wrong here since it does not paint the so-called non-client area of the form, e.g. the border and the title bar.
To hide the title bar, you need to set the ControlBox
property to false
and clear the form's Text
property. Set the border to FixedDialog
to make the form unresizable.
To retain the ability to move the form without the title bar, 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);
}
Basically this is the standard way of handling WM_NCHITTEST message and cheating, saying - the mouse cursor is on the window's caption [return value 0x2], so you will be able to move your form even if you click in the client area and drag it.
My problem was to have a resizable form with a thin border.
I set FormBorderStyle to None
I use a docked panel who contains all my controls.
I use the panel padding to set my borders width.
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 (e.X - panResize.Width > -16 && e.Y - panResize.Height > -16)
panResize.Cursor = Cursors.SizeNWSE;
else if (e.X - panResize.Width > -16)
panResize.Cursor = Cursors.SizeWE;
else if (e.Y - 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;
}
As no more information seems available, I will leave the border as suggested, set to FixedDialog with ControlBox property set to false and the form's Text cleared. I would prefer another color for the border and the ability to move the window though. Anyway thanks a lot for the answers.
精彩评论