Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Drag and Resize - Borderless Form

Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
17 Dec 2012CPOL1 min read 51.1K   5.2K   8   5
This code allows to drag and resize a borderless form!

Introduction

Everyone who makes Window apps knows how important a good-looking Formbox is. You might have also played around with the FormBorderStyle properties, with FormBorderStyle - NONE to create your individual Formbox. 

But you discover very quickly that such a Formbox will not be dragable or resizable. In this article we will see how to hardcode to achieve the same Formbox behaviors like on the default ones. 

Using the code 

Requirements for dragging a Formbox:

  • One Panel: Meant to drag the form window around. Choose for Panel the anchors left and right (only).
  • Three TextBoxes: The Close button, the Maximize button, and the Minimize button.
C#
// ****************************************
// * 		    Created by bEGI	  *
// *  http://www.youtube.com/user/MCneun  *
// *		   VB & C# Programmer	  *
// ****************************************

using System;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;


namespace Borderless_Form
{
    public partial class Borderless : Form
    {
        public Borderless()
        {
            InitializeComponent();
        }  

We declare two integers which hold the values of the mouse position coordinates, and a Boolean which turns true when the mouse is pressed down on the panel area:

C#
#region 'Drag'
     
int posX;
int posY;
bool drag;

private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        if (maximized)
        {
            this.WindowState = FormWindowState.Normal;
            maximized = false;
        }
        else
        {
            this.WindowState = FormWindowState.Maximized;
            maximized = true;
        }
    }
}

private void buttonClos_Click(object sender, EventArgs e)
{
    this.Close();
}

private void buttonMax_Click(object sender, EventArgs e)
{
    if (maximized)
    {
        maximized = false;
        this.WindowState = FormWindowState.Normal;
    }
    else
    {
        maximized = true;
        this.WindowState = FormWindowState.Maximized;
    }
}

private void buttonMin_Click(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Minimized;
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        drag = true;
        posX = Cursor.Position.X - this.Left;
        posY = Cursor.Position.Y - this.Top;
    }
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
    drag = false;
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (drag)
    {
        this.Top = System.Windows.Forms.Cursor.Position.Y - posY;
        this.Left = System.Windows.Forms.Cursor.Position.X - posX;
    }
    this.Cursor = Cursors.Default;
}

#endregion

We need also a Timer which cares when we resize the window thus it auto-arranges the Panel and the Buttons to be always on the proportional desired position. 

C#
private void timer1_Tick(object sender, EventArgs e)
{
    panel1.Width = this.Width - 6;
    panel1.Location = new Point(3, 3);
    buttonClos.Location = new Point(panel1.Width - 23, 3);
    buttonMax.Location = new Point(panel1.Width - 43, 3);
    buttonMin.Location = new Point(panel1.Width - 63, 3);
}

Requirements for resizing a Formbox:

We need the help of events to allow us creating a complex Windows app. In this case we need to catch: Mouse Up, Mouse Down, and Mouse Move events.

C#
#region 'Resize'
       
        bool maximized;
        bool on_MinimumSize;
        short minimumWidth = 350;
        short minimumHeight = 26;
        short borderSpace = 20;
        short borderDiameter = 3;

        bool onBorderRight;
        bool onBorderLeft;
        bool onBorderTop;
        bool onBorderBottom;
        bool onCornerTopRight;
        bool onCornerTopLeft;
        bool onCornerBottomRight; 
        bool onCornerBottomLeft; 

        bool movingRight;
        bool movingLeft;
        bool movingTop;
        bool movingBottom;
        bool movingCornerTopRight;
        bool movingCornerTopLeft;
        bool movingCornerBottomRight;
        bool movingCornerBottomLeft;

        private void Borderless_MouseUp(object sender, MouseEventArgs e)
        {
            stopResizer();
        }

        private void Borderless_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (onBorderRight ) { movingRight = true; } else { movingRight = false; }
                if (onBorderLeft) { movingLeft = true;} else { movingLeft = false; }
                if (onBorderTop) { movingTop = true;} else { movingTop = false; }
                if (onBorderBottom) { movingBottom = true; } else { movingBottom = false; }
                if (onCornerTopRight) { movingCornerTopRight = true;} else { movingCornerTopRight = false; }
                if (onCornerTopLeft) { movingCornerTopLeft = true; } else { movingCornerTopLeft = false; }
                if (onCornerBottomRight) { movingCornerBottomRight = true;} else { movingCornerBottomRight = false; }
                if (onCornerBottomLeft) { movingCornerBottomLeft = true; } else { movingCornerBottomLeft = false; }
            }
        }

        private void Borderless_MouseMove(object sender, MouseEventArgs e)
        {
            if (maximized) { return; }

            if (this.Width <= minimumWidth) { this.Width = (minimumWidth + 5); on_MinimumSize = true; }
            if (this.Height <= minimumHeight) { this.Height = (minimumHeight + 5); on_MinimumSize = true; }
            if (on_MinimumSize) { stopResizer(); } else { startResizer(); }

    
            if ((Cursor.Position.X > ((this.Location.X + this.Width) - borderDiameter))
                & (Cursor.Position.Y > (this.Location.Y + borderSpace))
                & (Cursor.Position.Y < ((this.Location.Y + this.Height) - borderSpace)))
            { this.Cursor = Cursors.SizeWE; onBorderRight = true; }

            else if ((Cursor.Position.X < (this.Location.X + borderDiameter))
                & (Cursor.Position.Y > (this.Location.Y + borderSpace))  
                & (Cursor.Position.Y < ((this.Location.Y + this.Height) - borderSpace)))
            { this.Cursor = Cursors.SizeWE; onBorderLeft = true; }

            else if ((Cursor.Position.Y < (this.Location.Y + borderDiameter))
                & (Cursor.Position.X > (this.Location.X + borderSpace))
                & (Cursor.Position.X < ((this.Location.X + this.Width) - borderSpace)))
            { this.Cursor = Cursors.SizeNS; onBorderTop = true; }

            else if ((Cursor.Position.Y > ((this.Location.Y + this.Height) - borderDiameter)) 
                & (Cursor.Position.X > (this.Location.X + borderSpace)) 
                & (Cursor.Position.X < ((this.Location.X + this.Width) - borderSpace)))
            { this.Cursor = Cursors.SizeNS; onBorderBottom = true; }

            else if ((Cursor.Position.X == ((this.Location.X + this.Width) - 1)) 
                & (Cursor.Position.Y == this.Location.Y))
            { this.Cursor = Cursors.SizeNESW; onCornerTopRight = true; }
            else if ((Cursor.Position.X == this.Location.X)
                & (Cursor.Position.Y == this.Location.Y))
            { this.Cursor = Cursors.SizeNWSE; onCornerTopLeft = true; }

            else if ((Cursor.Position.X == ((this.Location.X + this.Width) - 1)) 
                & (Cursor.Position.Y == ((this.Location.Y + this.Height) - 1)))
            { this.Cursor = Cursors.SizeNWSE; onCornerBottomRight = true; }

            else if ((Cursor.Position.X == this.Location.X)
                & (Cursor.Position.Y == ((this.Location.Y + this.Height) - 1)))
            { this.Cursor = Cursors.SizeNESW; onCornerBottomLeft = true; }

            else
            {
                onBorderRight = false;
                onBorderLeft = false;
                onBorderTop = false;
                onBorderBottom = false;
                onCornerTopRight = false;
                onCornerTopLeft = false;
                onCornerBottomRight = false;
                onCornerBottomLeft = false;
                this.Cursor = Cursors.Default;
            }
        }

        private void startResizer()
        {
            if (movingRight)
            {
                this.Width = Cursor.Position.X - this.Location.X;
            }    
    
            else if (movingLeft)
            {
                this.Width = ((this.Width + this.Location.X) - Cursor.Position.X);
                this.Location = new Point(Cursor.Position.X, this.Location.Y);
            }  
  
            else if (movingTop)
            {
                this.Height = ((this.Height + this.Location.Y) - Cursor.Position.Y);
                this.Location = new Point(this.Location.X, Cursor.Position.Y);
            }   
  
            else if (movingBottom)
            {
                this.Height = (Cursor.Position.Y - this.Location.Y);
            }  
  
            else if (movingCornerTopRight)
            {
                this.Width = (Cursor.Position.X - this.Location.X);
                this.Height = ((this.Location.Y - Cursor.Position.Y) + this.Height);
                this.Location = new Point(this.Location.X, Cursor.Position.Y);
            } 

            else if  (movingCornerTopLeft)
            {
                this.Width = ((this.Width + this.Location.X) - Cursor.Position.X);
                this.Location = new Point(Cursor.Position.X, this.Location.Y);
                this.Height = ((this.Height + this.Location.Y) - Cursor.Position.Y);
                this.Location = new Point(this.Location.X, Cursor.Position.Y);
            } 
 
            else if (movingCornerBottomRight)
            {
                this.Size = new Size(Cursor.Position.X - this.Location.X, 
                                     Cursor.Position.Y - this.Location.Y);
            }

            else if (movingCornerBottomLeft)
            {
                this.Width = ((this.Width + this.Location.X) - Cursor.Position.X);
                this.Height = (Cursor.Position.Y - this.Location.Y);
                this.Location = new Point(Cursor.Position.X, this.Location.Y);
            }          
        }

        private void stopResizer()
        {
            movingRight = false;
            movingLeft = false;
            movingTop = false;
            movingBottom = false;
            movingCornerTopRight = false;
            movingCornerTopLeft = false;
            movingCornerBottomRight = false;
            movingCornerBottomLeft = false;
            this.Cursor = Cursors.Default;
            System.Threading.Thread.Sleep(300);
            on_MinimumSize = false;
        }
        #endregion 
    }
}

History

  • 18 December, 2012: Edited and added C# source.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionLittle simpler these days. Pin
ChizI9-Jun-22 14:49
ChizI9-Jun-22 14:49 
QuestionIS possible do it but with mdicontainer in true Pin
Member 1374779626-Mar-18 9:12
Member 1374779626-Mar-18 9:12 
QuestionResize form? Pin
Member 1192764128-Aug-15 5:31
Member 1192764128-Aug-15 5:31 
QuestionWhy the timer? Pin
rocky359824-Aug-13 14:40
rocky359824-Aug-13 14:40 
QuestionMy Vote 5! Pin
Ibrahim Dhanani10-Jun-13 22:16
Ibrahim Dhanani10-Jun-13 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.