Making A Draggable, Borderless Form C#

27 June, 2019

Step 1 - Adding Event

Go into the events of either the whole form or on a control/panel and add the MouseDown event.

MouseDown Event

Step 2 - Variables & Importing Namespace

Import the namespace:

using System.Runtime.InteropServices;

Define the following:

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();

Step 3 - Make Event

Put the following lines of code inside of your event (Change TopPanel_MouseDown to what you called your event above):

private void TopPanel_MouseDown(object sender, MouseEventArgs e)
{
    ReleaseCapture();
    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}

Done! 🎉

Now when you click, hold and move your mouse on whatever you put this event on, your form will move!