Click here to Skip to main content
6,305,776 members and growing! (16,889 online)
Email Password   helpLost your password?
Languages » C# » Utilities     Intermediate License: The Code Project Open License (CPOL)

Draggable Form: Drag a Borderless Form by Clicking Anywhere on the Form

By Russell Aboobacker

Drag a borderless form by clicking anywhere on the form
C#, Windows, .NET, Visual Studio, Dev
Posted:25 Mar 2006
Updated:7 Apr 2006
Views:35,745
Bookmarked:13 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
10 votes for this article.
Popularity: 4.42 Rating: 4.42 out of 5
2 votes, 20.0%
1

2

3
2 votes, 20.0%
4
6 votes, 60.0%
5
Sample image

Introduction

This is useful when you have a form without any border and you want to allow the user to drag it. This is a simple code and doesn't require any explanation at all. But I feel this will be helpful for at least some of you for your development. If you set the BorderStyle of a form to None, no title bar will be available for you to move the form. Here you can make use of this simple code to setup a base form and inherit all your form from this base form. After inheriting from this base form, you will be provided with two more properties in the Property Editor, Draggable (boolean) and ExcludeList (String). By setting the Draggable property to true, you are enabling the draggable feature of the form. You can pass a comma separated list of controls for which you don't want the draggable feature to be enabled. That is by default if Draggable=true, the form will be draggable by clicking anywhere on the form and any controls on the form. And naturally, you don't want the form draggable while a user clicks on a Button on it. So you can pass the name of the button to this list. E.g.: ExcludeList = "button1, button2, button3";.

FormBase.cs

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

namespace DraggableForm
{    
    public class FormBase :Form
    {
        #region Declarations
        private bool drag = false;
        private Point start_point = new Point(0, 0);
        private bool draggable = true;
        private string exclude_list = "";

        /// <SUMMARY>
        /// Required designer variable.
        /// </SUMMARY>
        private System.ComponentModel.IContainer components = null;
        #endregion

        #region Constructor , Dispose

        public FormBase()
        {
            InitializeComponent();

        //
        //Adding Mouse Event Handlers for the Form
        //
        this.MouseDown += new MouseEventHandler(Form_MouseDown);
            this.MouseUp += new MouseEventHandler(Form_MouseUp);
            this.MouseMove += new MouseEventHandler(Form_MouseMove);
        }     

        /// <SUMMARY>
        /// Clean up any resources being used.
        /// </SUMMARY>
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #endregion

        #region Windows Form Designer generated code

        /// <SUMMARY>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </SUMMARY>
        private void InitializeComponent()
        {
            // 
            // FormBase
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(369, 182);
            this.Name = "FormBase";
            this.Text = "AlerterForm";
        }

        #endregion

        #region Overriden Functions

        protected override void OnControlAdded(ControlEventArgs e)
        {
            //
            //Add Mouse Event Handlers for each control added into the form,
            //if Draggable property of the form is set to true and the control
            //name is not in the ExcludeList.Exclude list is the comma separated
            //list of the Controls for which you do not require the mouse handler 
            //to be added. For Example a button.  
            //
            if (this.Draggable && (this.ExcludeList.IndexOf(e.Control.Name) == -1))
            {
                e.Control.MouseDown += new MouseEventHandler(Form_MouseDown);
                e.Control.MouseUp += new MouseEventHandler(Form_MouseUp);
                e.Control.MouseMove += new MouseEventHandler(Form_MouseMove);
            }
            base.OnControlAdded(e);
        }

        #endregion

        #region Event Handlers

        void Form_MouseDown(object sender, MouseEventArgs e)
        {          
            //
            //On Mouse Down set the flag drag=true and 
            //Store the clicked point to the start_point variable
            //
            this.drag = true;            
            this.start_point = new Point(e.X, e.Y);
        }

        void Form_MouseUp(object sender, MouseEventArgs e)
        {
            //
            //Set the drag flag = false;
            //
            this.drag = false;
        }

        void Form_MouseMove(object sender, MouseEventArgs e)
        {
            //
            //If drag = true, drag the form
            //
            if (this.drag)
            {
                Point p1 = new Point(e.X, e.Y);
                Point p2 = this.PointToScreen(p1);
                Point p3 = new Point(p2.X - this.start_point.X, 
                                     p2.Y - this.start_point.Y);
                this.Location = p3;
            }
        }

        #endregion

        #region Properties

        public string ExcludeList
        {
            set
            {
                this.exclude_list = value;
            }
            get
            {
                return this.exclude_list.Trim();
            }
        }

        public bool Draggable
        {
            set
            {
                this.draggable = value;
            }
            get
            {
                return this.draggable;
            }
        }

        #endregion
    }
}

Now, you can simply inherit your form from the FormBase and voila, it'll be draggable. If you have any suggestions, please mail me. I love to have friends and that's the reason I am here. Happy coding!

License

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

About the Author

Russell Aboobacker


Member
Russell Aboobacker is a Software Engineer from India, Currently working in First Indian Corp Bangalore as Senior Software Engineer. He Enjoys Coding and Sharing his Experience with the Colleagues and Friends.When he is not coding he enjoys spending time with his Beloved Wife and Son.

If you have any suggestions / Ideas , Share it With me. arussel@firstam.com
Occupation: Web Developer
Location: India India

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralRemove handles to move PinmemberCFQüeb9:30 2 Apr '09  
GeneralRemoving the handles to move the form PinmemberCFQüeb9:27 2 Apr '09  
GeneralHow to move or drag a borderless form in vb.net PinmemberThe Ricmann2:35 14 Aug '08  
GeneralVery good for widgets and resx question Pinmembermilkplus6:39 7 Sep '07  
GeneralTearing/Messy Painting Pinmemberxr280xr8:58 27 Aug '07  
GeneralThe Shortest Way PinmemberAnt Htoo Naing9:43 6 Jul '06  
GeneralThe Shortest Way (in VB.NET) PinmemberAnt Htoo Naing9:52 6 Jul '06  
GeneralRe: The Shortest Way (in VB.NET) Pinmemberkaushik25910621:33 28 Mar '07  
GeneralA Shorter and Better Way Pinmembergeorani4:27 26 Mar '06  
GeneralConverted to VB 2005 ( More Shorter Yet) Pinmembergeorani4:42 26 Mar '06  
GeneralRe: Converted to VB 2005 ( More Shorter Yet) PinmemberVäinölä Harri20:52 9 Apr '06  
GeneralRe: Converted to VB 2005 ( More Shorter Yet) Pinmembergeorani1:17 10 Apr '06  
GeneralWhen the FormBorderStyle is not equals to FormBorderStyle.None Pinmembergeorani2:22 26 Mar '06  
GeneralGood job. PinmemberVlad Stanciu20:57 25 Mar '06  
GeneralAlternate method PinmemberRavi Bhavnani8:04 25 Mar '06  
GeneralRe: Alternate method PinmemberThe_Mega_ZZTer8:12 25 Mar '06  
GeneralRe: Alternate method PinmemberRussell Aboobacker6:32 26 Mar '06  
AnswerRe: Alternate method PinmemberRavi Bhavnani6:45 26 Mar '06  
GeneralThis code is wrong, see the correct Pinmembergeorani1:22 27 Mar '06  
GeneralRe: This code is wrong, see the correct PinmemberRavi Bhavnani2:00 27 Mar '06  
GeneralRe: This code is wrong, see the correct PinmemberRussell Aboobacker2:57 28 Mar '06  
GeneralRe: This code is wrong, see the correct PinmemberCFQüeb8:25 2 Apr '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Apr 2006
Editor: Deeksha Shenoy
Copyright 2006 by Russell Aboobacker
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project