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

How to Move a Windows Form By Dragging

By , 10 Jan 2014
Rate this:
Please Sign up or sign in to vote.


Today, I was working on a Desktop application where I was required to drag a form by Mouse left click as we do in normal drag and drop operations. the only difference in this scenario was I needed to drag the entire form and the form was not supposed to be dropped anywhere else.

I found a very easy way to implement this. This can be achieved using 2 ways – Handling Mouse Events of the form or by using Interop assemblies. Using Mouse Events to handle the drag operation is a bit messy and does not work to 100% perfection. In this post, we will see how we can add this feature to our Windows Form. To do this we will follow the below steps. Let’s assume the form name is Form1 -

  • Set the FormBorderStyle of the form to None.
  • Include the Assembly System.Runtime.InteropServices on Form1.cs page
  • Declare below variables
  • public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    public static extern bool ReleaseCapture();
  • Handle the MouseDown event of the form
  • Write the below code in the Event Handler.
  • private void Form1_MouseDown(object sender, MouseEventArgs e)
                if (e.Button == MouseButtons.Left)
                    SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);

You’re done! Run the code and you will able to drag a form easily with your mouse left click. Hope you like this post! Cheers!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Nitesh Luharuka
Team Leader Independent IT Professional, Consultant
India India
An Independent IT Professional and Consultant on MS Technologies like .Net | MVC | Silverlight | Sharepoint | MS Project Server along with open source CMS Systems like DotNetNuke etc.
I am an enthusiastic technocrat who love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks.
Follow on   Twitter   LinkedIn

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 11 Jan 2014
Article Copyright 2014 by Nitesh Luharuka
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid