views:

36

answers:

2

Hi, 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 = 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.

+1  A: 

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 ControlBoxproperty 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.

liggett78
The OnPaint method is fine, he set the FormBorderStyle to None.
Hans Passant
Thank you, it helps.
Pierre
Though, the code for the move is not working, case is missing and Result is IntPtr, it cannot be assigned 0x2. Besides, I would like to change the size and the color of the border.
Pierre
So, can anybody help with these 2 problems left : moving the window and changing the size and color of the border ? Thanks in advance.
Pierre
A: 

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.

Pierre