Click here to Skip to main content
14,331,865 members

Making a Borderless Form Movable

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

Introduction

This article defines a way to make a borderless 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 borderless. This can easily be done by setting the FormBorderStyle property of this form to None.

Image 1

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

Image 2

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.

Image 3

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 presses 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 keeps 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 borderless and movable too.

Points of Interest

There are many functions that can be used in a 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

  • 19th 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)

Share

About the Author

Rahul Rajat Singh
Architect
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): Accessing Data 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[^]

  • Microsoft MVP 2015

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 919984326-Aug-15 9:52
memberMember 919984326-Aug-15 9:52 
QuestionIn WPF this is covered natively Pin
Miroxlav16-Feb-14 21:57
memberMiroxlav16-Feb-14 21:57 
GeneralMy vote of 5 Pin
wangxj_nemo10-Jan-13 15:49
memberwangxj_nemo10-Jan-13 15:49 

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.

Alternative
Article
Posted 29 Mar 2012

Stats

45.5K views
1.6K downloads
10 bookmarked