Click here to Skip to main content
15,884,836 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 403.3K   7.1K   85   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

 
QuestionSlick! Short and to the point. Pin
AVIDeveloper27-Jun-11 6:17
AVIDeveloper27-Jun-11 6:17 
GeneralDragmove Pin
stage616-May-11 6:02
stage616-May-11 6:02 
GeneralMy vote of 5 Pin
deep124519-Feb-11 1:37
deep124519-Feb-11 1:37 
GeneralMy vote of 5 Pin
shanawazway23-Jan-11 19:21
shanawazway23-Jan-11 19:21 
GeneralSlight modification for WPF :) Pin
xhutchinson17-Jan-11 18:45
xhutchinson17-Jan-11 18:45 
GeneralRe: Slight modification for WPF :) Pin
AhmyAM10-Feb-14 5:18
AhmyAM10-Feb-14 5:18 
GeneralMy vote of 5 Pin
jingobaba30-Dec-10 1:32
jingobaba30-Dec-10 1:32 
GeneralMy vote of 5 Pin
Francisco Soto23-Jul-10 15:01
Francisco Soto23-Jul-10 15:01 
excelent! very good!
GeneralDirectShow + WM_NCLBUTTONDOWN Pin
SolarAngel5-Mar-10 14:01
SolarAngel5-Mar-10 14:01 
GeneralRight Mouse Button Pin
IAbstract18-Dec-09 8:06
IAbstract18-Dec-09 8:06 
GeneralRe: Right Mouse Button Pin
LimitedAtonement19-Nov-11 14:11
LimitedAtonement19-Nov-11 14:11 
GeneralGreat Pin
MrKhal28-Nov-09 2:36
MrKhal28-Nov-09 2:36 
Generalgreat but Pin
AntounPG28-Nov-09 0:08
AntounPG28-Nov-09 0:08 
NewsReviews [modified] Pin
Focuscar5-Aug-09 4:25
Focuscar5-Aug-09 4:25 
Questionthank's for you greate article ! Pin
RNematjon30-Jul-09 1:00
RNematjon30-Jul-09 1:00 
Generalthanks champ Pin
Jethro874-Jun-09 8:35
Jethro874-Jun-09 8:35 
GeneralGreat! Pin
emboole4-Jun-09 3:03
emboole4-Jun-09 3:03 
GeneralVery thank you. Pin
v# guy2-Jun-09 17:07
v# guy2-Jun-09 17:07 
GeneralThanks!!! Pin
RazorHaven20-May-09 7:31
RazorHaven20-May-09 7:31 
GeneralThanks much Pin
kennygmaina4-Feb-09 23:03
kennygmaina4-Feb-09 23:03 
GeneralBaaaaaaam! Pin
idrivefastlane19-Jan-09 22:08
idrivefastlane19-Jan-09 22:08 
GeneralWPARAM and LPARAM != int Pin
dataman64bit6-Aug-08 12:37
dataman64bit6-Aug-08 12:37 
Generalmove to next form Pin
kanza azhar4-Jun-08 11:58
kanza azhar4-Jun-08 11:58 
GeneralRe: move to next form Pin
kanza azhar4-Jun-08 12:02
kanza azhar4-Jun-08 12:02 
GeneralSmart code, thanks! Pin
Member 183173730-Mar-08 7:29
Member 183173730-Mar-08 7:29 

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.