Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / C#
Article

Move window/form without Titlebar in C#

Rate me:
Please Sign up or sign in to vote.
4.58/5 (92 votes)
26 Jul 2005GPL3 404.5K   7.1K   84   75
How to move a form without having a Titlebar in C#.

Introduction

This is my first article and I hope my English is OK. Have you ever wanted to move a window without a Titlebar in C#? Well, then you're right here.

Let's start

First you have to add the following code in the header of your project. This is needed to load the required DLL's to our project. According to MSDN ReleaseCapture: removes mouse capture from the object in the current document and SendMessage: sends the specified message to a window or windows. It calls the window procedure for the specified window and does not return until the window procedure has processed the message.

C#
using System.Runtime.InteropServices;

If you've done this, you can add the const and the DLL functions:

C#
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();

Then put the following two lines of code in the form's MouseDown event like this:

C#
private void Form1_MouseDown(object sender, 
        System.Windows.Forms.MouseEventArgs e)
{     
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
}

Believe it or not - you've done it! Now run the App and try to move the form by clicking on the form and moving the mouse (you have to hold the button pressed!)

Hope you find it useful. Enjoy!

Thanks to Patric_J and John Wood for their replies! The code has been updated!

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Germany Germany
Born on August 6th 1982 in Neuss (NRW in Germany). Currently i'm 22 years old guy and my Hobby's are programming, bicycle and swim.

Comments and Discussions

 
GeneralStarting Pin
sreejith ss nair26-Jul-05 18:00
sreejith ss nair26-Jul-05 18:00 
GeneralRe: Starting Pin
FreewareFire27-Jul-05 0:19
FreewareFire27-Jul-05 0:19 
QuestionA better way? Pin
Axel Rietschin26-Jul-05 16:40
professionalAxel Rietschin26-Jul-05 16:40 
AnswerRe: A better way? Pin
FreewareFire27-Jul-05 0:16
FreewareFire27-Jul-05 0:16 
AnswerRe: A better way? Pin
Tim McCurdy6-Sep-05 17:31
Tim McCurdy6-Sep-05 17:31 
GeneralRe: A better way? Pin
Robertica18-Feb-07 22:51
Robertica18-Feb-07 22:51 
QuestionRe: A better way? Pin
Lord of Scripts18-Jun-07 3:18
Lord of Scripts18-Jun-07 3:18 
GeneralJust a warning if your form has a context menu Pin
Patric_J26-Jul-05 10:34
Patric_J26-Jul-05 10:34 
In that case you should only fake the captionbar click for left mouse button clicks, since the right mouse button is to bring up the context menu

private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
}


My C# blog: C# Coach
GeneralRe: Just a warning if your form has a context menu Pin
FreewareFire26-Jul-05 11:16
FreewareFire26-Jul-05 11:16 
GeneralGood Pin
John Wood26-Jul-05 10:05
John Wood26-Jul-05 10:05 
GeneralRe: Good Pin
FreewareFire26-Jul-05 11:17
FreewareFire26-Jul-05 11:17 

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.