Click here to Skip to main content
Licence GPL3
First Posted 26 Jul 2005
Views 147,792
Downloads 1,399
Bookmarked 67 times

Move window/form without Titlebar in C#

By FreewareFire | 26 Jul 2005
How to move a form without having a Titlebar in C#.
4 votes, 6.8%
1
2 votes, 3.4%
2
6 votes, 10.2%
3
6 votes, 10.2%
4
41 votes, 69.5%
5
4.29/5 - 59 votes
4 removed
μ 3.96, σa 2.13 [?]

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
GeneralMy vote of 5 PinmemberNeoshao5:52 23 Dec '11  
GeneralMy vote of 3 PinmemberLimitedAtonement15:12 19 Nov '11  
GeneralMy vote of 5 PinmemberHoria Tudosie3:33 6 Oct '11  
QuestionSlick! Short and to the point. PinmemberAvi Bueno7:17 27 Jun '11  
GeneralDragmove Pinmemberstage67:02 16 May '11  
GeneralMy vote of 5 Pinmemberdeep12452:37 19 Feb '11  
GeneralMy vote of 5 Pinmembershanawazway20:21 23 Jan '11  
GeneralSlight modification for WPF :) Pinmemberxhutchinson19:45 17 Jan '11  
GeneralMy vote of 5 Pinmemberjingobaba2:32 30 Dec '10  
GeneralMy vote of 5 PinmemberFrancisco Soto16:01 23 Jul '10  
GeneralDirectShow + WM_NCLBUTTONDOWN PinmemberSolarAngel15:01 5 Mar '10  
GeneralRight Mouse Button Pinmemberddboarm9:06 18 Dec '09  
GeneralRe: Right Mouse Button PinmemberLimitedAtonement15:11 19 Nov '11  
GeneralGreat PinmemberMrKhal3:36 28 Nov '09  
Generalgreat but PinmemberAntoun Gorgy1:08 28 Nov '09  
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  

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