Click here to Skip to main content
12,505,335 members (73,697 online)
Click here to Skip to main content
Articles » Languages » VB.NET » Windows Forms » Downloads

Stats

22.3K views
3.1K downloads
6 bookmarked
Posted

Drag and Resize - Borderless Form

, 17 Dec 2012 CPOL
This code allows to drag and resize a borderless form!
Borderless Form
Borderless Form
bin
Release
Borderless Form.vshost.exe.manifest
Properties
Resources
close2.bmp
maximize2.bmp
minimize2.bmp
Orange.bmp
Borderless Form.suo
Debug
Borderless Form.exe
Borderless Form.pdb
Borderless Form.vshost.exe
Borderless Form.exe
Borderless Form.pdb
Borderless Form.vshost.exe
Borderless Form.vshost.exe.manifest
obj
x86
Debug
Borderless Form.exe
Borderless Form.pdb
Borderless_Form.Borderless.resources
Borderless_Form.Properties.Resources.resources
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource-ResGen.read.1.tlog
GenerateResource-ResGen.read.6.tlog
GenerateResource-ResGen.write.1.tlog
TempPE
Properties.Resources.Designer.cs.dll
Release
Borderless Form.exe
Borderless Form.pdb
Borderless_Form.Borderless.resources
Borderless_Form.Properties.Resources.resources
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource-ResGen.read.1.tlog
GenerateResource-ResGen.read.6.tlog
GenerateResource-ResGen.write.1.tlog
TempPE
Properties.Resources.Designer.cs.dll
close2.bmp
maximize2.bmp
minimize2.bmp
Orange.bmp
Borderless Form.suo
Borderless Form.vshost.exe.manifest
My Project
Application.myapp
close2.bmp
maximize2.bmp
minimize2.bmp
Orange.bmp
Borderless Form.suo
Borderless Form.exe
Borderless Form.pdb
Borderless Form.vshost.exe
Borderless Form.exe
Borderless Form.pdb
Borderless Form.vshost.exe
Borderless Form.vshost.exe.manifest
Application.myapp
Borderless Form.exe
Borderless Form.pdb
Borderless_Form.Borderless.resources
Borderless_Form.Resources.resources
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource-ResGen.read.1.tlog
GenerateResource-ResGen.read.4.tlog
GenerateResource-ResGen.write.1.tlog
My Project.Resources.Designer.vb.dll
Borderless Form.exe
Borderless Form.pdb
Borderless_Form.Borderless.resources
Borderless_Form.Resources.resources
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource-ResGen.read.1.tlog
GenerateResource-ResGen.read.4.tlog
GenerateResource-ResGen.write.1.tlog
My Project.Resources.Designer.vb.dll
close2.bmp
maximize2.bmp
minimize2.bmp
Orange.bmp
Borderless Form.suo
/// ****************************************
/// * 		    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();
        }

        #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

        #region 'Resize'
       
        bool onFullScreen;
        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 (onFullScreen | 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

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

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author


You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160927.1 | Last Updated 18 Dec 2012
Article Copyright 2012 by bEGI23
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid