Click here to Skip to main content
Licence GPL3
First Posted 26 Jul 2005
Views 155,283
Bookmarked 67 times

Move window/form without Titlebar in C#

By | 26 Jul 2005 | Article
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

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHi... Pinmembernileshbahirshet19:56 9 May '12  
AnswerRe: Hi... PinmemberFreewareFire10:19 10 May '12  
GeneralMy vote of 5 PinmemberNeoshao4:52 23 Dec '11  
GeneralMy vote of 3 PinmemberLimitedAtonement14:12 19 Nov '11  
GeneralMy vote of 5 PinmemberHoria Tudosie2:33 6 Oct '11  
QuestionSlick! Short and to the point. PinmemberAvi Bueno6:17 27 Jun '11  
GeneralDragmove Pinmemberstage66:02 16 May '11  
GeneralMy vote of 5 Pinmemberdeep12451:37 19 Feb '11  
GeneralMy vote of 5 Pinmembershanawazway19:21 23 Jan '11  
GeneralSlight modification for WPF :) Pinmemberxhutchinson18:45 17 Jan '11  
GeneralMy vote of 5 Pinmemberjingobaba1:32 30 Dec '10  
GeneralMy vote of 5 PinmemberFrancisco Soto15:01 23 Jul '10  
GeneralDirectShow + WM_NCLBUTTONDOWN PinmemberSolarAngel14:01 5 Mar '10  
GeneralRight Mouse Button Pinmemberddboarm8:06 18 Dec '09  
GeneralRe: Right Mouse Button PinmemberLimitedAtonement14:11 19 Nov '11  
GeneralGreat PinmemberMrKhal2:36 28 Nov '09  
Generalgreat but PinmemberAntoun Gorgy0:08 28 Nov '09  
NewsReviews [modified] PinmemberGoS-ExiGo4:25 5 Aug '09  
Questionthank's for you greate article ! PinmemberRNematjon1:00 30 Jul '09  
Generalthanks champ PinmemberJethro878:35 4 Jun '09  
GeneralGreat! Pinmemberemboole3:03 4 Jun '09  
GeneralVery thank you. PinmemberV# Guy17:07 2 Jun '09  
GeneralThanks!!! PinmemberRazorHaven7:31 20 May '09  
GeneralThanks much Pinmemberkennygmaina23:03 4 Feb '09  
GeneralBaaaaaaam! Pinmemberidrivefastlane22:08 19 Jan '09  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 26 Jul 2005
Article Copyright 2005 by FreewareFire
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid