Click here to Skip to main content
Licence GPL3
First Posted 26 Jul 2005
Views 155,690
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
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  
GeneralWPARAM and LPARAM != int Pinmemberdataman64bit12:37 6 Aug '08  
Both WPARAM and LPARAM are 8B on Windows x64 - therefore int is small and your program fail on such a system when using 64bit .NET framework. You should use IntPtr for wParam and lPARAM too.
Generalmove to next form Pinmemberkanza azhar11:58 4 Jun '08  
GeneralRe: move to next form Pinmemberkanza azhar12:02 4 Jun '08  
GeneralSmart code, thanks! PinmemberMember 18317377:29 30 Mar '08  
GeneralGr8 Pinmembervachan23:14 18 Sep '07  
GeneralNot in mouse down event PinmemberAlexanderBraun4:20 5 Jan '06  
AnswerRe: Not in mouse down event PinmemberFreewareFire11:33 8 Jan '06  
QuestionRe: Not in mouse down event PinmemberAJafer2:19 6 Sep '07  
QuestionRe: Not in mouse down event Pinmemberjury_mart23:01 19 May '08  
QuestionRe: Not in mouse down event Pinmembercraigk8:12 8 Apr '09  
Generalmove other window PinmemberKaczy12:38 19 Dec '05  
AnswerRe: move other window PinmemberFreewareFire11:32 8 Jan '06  
GeneralAnother method & Extending the article PinmemberJockerSoft21:10 3 Aug '05  
GeneralRe: Another method & Extending the article PinmemberFreewareFire2:59 7 Sep '05  
GeneralRe: Another method & Extending the article PinmemberJockerSoft20:48 7 Sep '05  
GeneralRe: Another method & Extending the article Pinmemberddboarm4:47 18 Dec '09  
GeneralOther option for dragging/resizing PinmemberRichard Brunet3:51 3 Aug '05  
GeneralRe: Other option for dragging/resizing. Oops! PinmemberRichard Brunet3:54 3 Aug '05  
GeneralResizing form with no frame Pinmemberphervers21:47 26 Jul '05  

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
Web03 | 2.5.120604.1 | Last Updated 26 Jul 2005
Article Copyright 2005 by FreewareFire
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid