First we will have to use the interop services by using the namespace as
using System.Runtime.InteropServices;
The next thing would be to define the messages that will take care of moving the form. We will have these as class member variables
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();
and finally we will write the code to send the message whenever the user presses the mouse button. The form will be repositioned as per the mouse movement if the user keep the mouse button pressed.
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
Refer this link Dragable form
Credits to rahul-rajat-singh