Click here to Skip to main content
Click here to Skip to main content
Alternative Article

Making a Borderless Form Movable

, 29 Mar 2012
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Making a Borderless Form Movable in C++"

Introduction

This article defines a way to make a border less form movable. There are many scenarios where we may want to remove the default windows border but still want the form to be movable. This article will explore one way of doing the same.

Background

I  read a similar article few days back here. I tried to do this myself long back. This article presents a very good way of moving the form but the logic for movement sometimes does not result in a smooth movement and sometimes the form flickers a lot while moving. So I put my preferred way of making a border less form movable and someone suggested I should have it as an alternative to this, Here I am presenting the alternative way of doing the same thing in this article.

Using the code

The first thing that we need to do is to make the form border less. this can easily be done by setting the FormBorderStyle property of this form to None.

After we set this property to null the Form will look like

The next step would be to handle the MouseDown event for the form. This event will contain the code that will perform the form movement operation when the mouse is pressed and dragged.

The basic idea behind this technique is to use the windows messages to handle the form movement. What we will do is whenever the user press the mouse button and moves the mouse we will send a message to move the form according to the mouse movement. For this we first need to have the possibility of sending the windows messages. First we will have to use the interop services by using the namespace as

using System.Runtime.InteropServices;

The next thing would be to define the messages that will take care of moving the form. We will have these as class member variables

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

and finally we will write the code to send the message whenever the user presses the mouse button. The form will be repositioned as per the mouse movement if the user keep the mouse button pressed.

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    ReleaseCapture();
    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
}

Now the form is border less and movable too.

Points of Interest

There are many functions that can be used in much more elegant manner by using the windows messages. Writing code ourselves could also be a good option but it is error prone and frankly "re-inventing the wheel". 

My article is in C# but the original article was meant for C++. but since it was basically for managed C++, The same code and philosophy will work without much/any changes.

History

19 March 2012: Posted an alternative article. the original article can be found here.

License

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

About the Author

Rahul Rajat Singh
Software Developer (Senior)
India India
I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.
  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4
 
If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionIn WPF this is covered natively PinmemberMiroxlav16-Feb-14 21:57 
GeneralMy vote of 5 Pinmemberwangxj_nemo10-Jan-13 15:49 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 29 Mar 2012
Article Copyright 2012 by Rahul Rajat Singh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid