Click here to Skip to main content
6,596,602 members and growing! (19,625 online)
Email Password   helpLost your password?
Languages » C# » Windows Forms     Intermediate

Move window/form without Titlebar in C#

By FreewareFire

How to move a form without having a Titlebar in C#.
C#, VC7.NET 1.1, WinXPVS.NET2003, Dev
Posted:26 Jul 2005
Views:82,641
Bookmarked:47 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
40 votes for this article.
Popularity: 5.79 Rating: 3.61 out of 5
5 votes, 12.5%
1
2 votes, 5.0%
2
4 votes, 10.0%
3
5 votes, 12.5%
4
24 votes, 60.0%
5

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

FreewareFire


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.
Location: Germany Germany

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 39 (Total in Forum: 39) (Refresh)FirstPrevNext
NewsReviews [modified] PinmemberGoS-ExiGo5:25 5 Aug '09  
Questionthank's for you greate article ! PinmemberRNematjon2:00 30 Jul '09  
Generalthanks champ PinmemberJethro879:35 4 Jun '09  
GeneralGreat! Pinmemberemboole4:03 4 Jun '09  
GeneralVery thank you. PinmemberV# Guy18:07 2 Jun '09  
GeneralThanks!!! PinmemberRazorHaven8:31 20 May '09  
GeneralThanks much Pinmemberkennygmaina0:03 5 Feb '09  
GeneralBaaaaaaam! Pinmemberidrivefastlane23:08 19 Jan '09  
GeneralWPARAM and LPARAM != int Pinmemberdataman64bit13:37 6 Aug '08  
Generalmove to next form Pinmemberkanza azhar12:58 4 Jun '08  
GeneralRe: move to next form Pinmemberkanza azhar13:02 4 Jun '08  
GeneralSmart code, thanks! PinmemberMember 18317378:29 30 Mar '08  
GeneralGr8 Pinmembervachan0:14 19 Sep '07  
GeneralNot in mouse down event PinmemberAlexanderBraun5:20 5 Jan '06  
AnswerRe: Not in mouse down event PinmemberFreewareFire12:33 8 Jan '06  
QuestionRe: Not in mouse down event PinmemberAJafer3:19 6 Sep '07  
QuestionRe: Not in mouse down event Pinmemberjury_mart0:01 20 May '08  
QuestionRe: Not in mouse down event Pinmembercraigk9:12 8 Apr '09  
Generalmove other window PinmemberKaczy13:38 19 Dec '05  
AnswerRe: move other window PinmemberFreewareFire12:32 8 Jan '06  
GeneralAnother method & Extending the article PinmemberJockerSoft22:10 3 Aug '05  
GeneralRe: Another method & Extending the article PinmemberFreewareFire3:59 7 Sep '05  
GeneralRe: Another method & Extending the article PinmemberJockerSoft21:48 7 Sep '05  
GeneralOther option for dragging/resizing PinmemberRichard Brunet4:51 3 Aug '05  
GeneralRe: Other option for dragging/resizing. Oops! PinmemberRichard Brunet4:54 3 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Jul 2005
Editor: Rinish Biju
Copyright 2005 by FreewareFire
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project