Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move and resize a form without a border?

Does anyone know how I can resize a winform when it has no border. I don't want the default border that Windows has, so I changed the property "FormBorderStyle" to "None". This removed the border, although now it can't be resized. I've figured out how to move the form around, I just need to know how to resize it.

like image 214
Joey Morani Avatar asked Apr 04 '10 17:04

Joey Morani


People also ask

How do I resize a form?

By dragging either the right edge, bottom edge, or the corner, you can resize the form. The second way you can resize the form while the designer is open, is through the properties pane. Select the form, then find the Properties pane in Visual Studio. Scroll down to size and expand it.

How do I make my windows form resizable?

To make a control resize with the form, set the Anchor to all four sides, or set Dock to Fill . It's fairly intuitive once you start using it. For example, if you have OK/Cancel buttons in the lower right of your dialog, set the Anchor property to Bottom and Right to have them drag properly on the form.


2 Answers

Some sample code that allow moving and resizing the form:

  public partial class Form1 : Form {     public Form1() {       InitializeComponent();       this.FormBorderStyle = FormBorderStyle.None;       this.DoubleBuffered = true;       this.SetStyle(ControlStyles.ResizeRedraw, true);     }     private const int cGrip = 16;      // Grip size     private const int cCaption = 32;   // Caption bar height;      protected override void OnPaint(PaintEventArgs e) {       Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);       ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);       rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);       e.Graphics.FillRectangle(Brushes.DarkBlue, rc);     }      protected override void WndProc(ref Message m) {       if (m.Msg == 0x84) {  // Trap WM_NCHITTEST         Point pos = new Point(m.LParam.ToInt32());         pos = this.PointToClient(pos);         if (pos.Y < cCaption) {           m.Result = (IntPtr)2;  // HTCAPTION           return;         }         if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip) {           m.Result = (IntPtr)17; // HTBOTTOMRIGHT           return;         }       }       base.WndProc(ref m);     }   } 
like image 196
Hans Passant Avatar answered Oct 01 '22 05:10

Hans Passant


Here's a complete example of a customized form with all 8 points of resizing:

public partial class Form1 : Form { public Form1() {   InitializeComponent();   this.FormBorderStyle = FormBorderStyle.None; // no borders   this.DoubleBuffered = true;   this.SetStyle(ControlStyles.ResizeRedraw, true); // this is to avoid visual artifacts }  protected override void OnPaint(PaintEventArgs e) // you can safely omit this method if you want {     e.Graphics.FillRectangle(Brushes.Green, Top);     e.Graphics.FillRectangle(Brushes.Green, Left);     e.Graphics.FillRectangle(Brushes.Green, Right);     e.Graphics.FillRectangle(Brushes.Green, Bottom); }  private const int     HTLEFT = 10,     HTRIGHT = 11,     HTTOP = 12,     HTTOPLEFT = 13,     HTTOPRIGHT = 14,     HTBOTTOM = 15,     HTBOTTOMLEFT = 16,     HTBOTTOMRIGHT = 17;  const int _ = 10; // you can rename this variable if you like  Rectangle Top { get { return new Rectangle(0, 0, this.ClientSize.Width, _); } } Rectangle Left { get { return new Rectangle(0, 0, _, this.ClientSize.Height); } } Rectangle Bottom { get { return new Rectangle(0, this.ClientSize.Height - _, this.ClientSize.Width, _); } } Rectangle Right { get { return new Rectangle(this.ClientSize.Width - _, 0, _, this.ClientSize.Height); } }  Rectangle TopLeft { get { return new Rectangle(0, 0, _, _); } } Rectangle TopRight { get { return new Rectangle(this.ClientSize.Width - _, 0, _, _); } } Rectangle BottomLeft { get { return new Rectangle(0, this.ClientSize.Height - _, _, _); } } Rectangle BottomRight { get { return new Rectangle(this.ClientSize.Width - _, this.ClientSize.Height - _, _, _); } }   protected override void WndProc(ref Message message) {     base.WndProc(ref message);      if (message.Msg == 0x84) // WM_NCHITTEST     {         var cursor = this.PointToClient(Cursor.Position);          if (TopLeft.Contains(cursor)) message.Result = (IntPtr)HTTOPLEFT;    else if (TopRight.Contains(cursor)) message.Result = (IntPtr)HTTOPRIGHT;    else if (BottomLeft.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMLEFT;    else if (BottomRight.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMRIGHT;     else if (Top.Contains(cursor)) message.Result = (IntPtr)HTTOP;    else if (Left.Contains(cursor)) message.Result = (IntPtr)HTLEFT;    else if (Right.Contains(cursor)) message.Result = (IntPtr)HTRIGHT;    else if (Bottom.Contains(cursor)) message.Result = (IntPtr)HTBOTTOM;     } }} 
like image 32
user1306322 Avatar answered Oct 01 '22 05:10

user1306322