Click here to Skip to main content
Click here to Skip to main content

Move window/form without Titlebar in C#

, 26 Jul 2005
Rate this:
Please Sign up or sign in to vote.
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.

using System.Runtime.InteropServices;

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

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:

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)

About the Author

FreewareFire

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

 
SuggestionAdd DragDetect() PinmemberKevinSW21hrs 36mins ago 
GeneralMy Vote of 5 Pinmemberfaysal_ubit27-Jun-14 20:55 
QuestionMessage Automatically Removed PinmemberMember 1023224425-Sep-13 23:23 
GeneralAlso works with single objects inside the form! PinmemberJ&Geckos24-Feb-13 10:07 
GeneralMy vote of 5 PinmemberJ&Geckos24-Feb-13 10:04 
QuestionC# Show Hide Windows Forms PinmemberTishantha18-Jan-13 4:41 
GeneralMy vote of 1000000000000000 PinmemberOraion13-Jan-13 15:12 
GeneralMy vote of 5 PinmemberSushant Shidore28-Oct-12 21:53 
QuestionHi... Pinmembernileshbahirshet9-May-12 19:56 
AnswerRe: Hi... PinmemberFreewareFire10-May-12 10:19 
GeneralMy vote of 5 PinmemberNeoshao23-Dec-11 4:52 
GeneralMy vote of 3 PinmemberLimitedAtonement19-Nov-11 14:12 
GeneralRe: My vote of 3 PinmemberNitesh Luharuka9-Jan-14 3:55 
GeneralMy vote of 5 PinmemberHoria Tudosie6-Oct-11 2:33 
QuestionSlick! Short and to the point. PinmemberAvi Bueno27-Jun-11 6:17 
GeneralDragmove Pinmemberstage616-May-11 6:02 
GeneralMy vote of 5 Pinmemberdeep124519-Feb-11 1:37 
GeneralMy vote of 5 Pinmembershanawazway23-Jan-11 19:21 
GeneralSlight modification for WPF :) Pinmemberxhutchinson17-Jan-11 18:45 
GeneralRe: Slight modification for WPF :) PinmemberAhmyAM10-Feb-14 5:18 
GeneralMy vote of 5 Pinmemberjingobaba30-Dec-10 1:32 
GeneralMy vote of 5 PinmemberFrancisco Soto23-Jul-10 15:01 
GeneralDirectShow + WM_NCLBUTTONDOWN PinmemberSolarAngel5-Mar-10 14:01 
GeneralRight Mouse Button Pinmemberddboarm18-Dec-09 8:06 
GeneralRe: Right Mouse Button PinmemberLimitedAtonement19-Nov-11 14:11 

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
Web02 | 2.8.140721.1 | Last Updated 26 Jul 2005
Article Copyright 2005 by FreewareFire
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid