Posts

Showing posts from January, 2016

Move form with mouse movement using mouse down event C#.NET

Sometimes due to design requirement we need to create a border less form or a custom border around the form.  In case of default border the movement of form on mouse down is handled internally we do not need to write any code for this. But if we create a custom border or a border-less form then in that case we need to write a custom code to handle form movement using mouse. Here is the C# code to handle form movement on mouse down. 
using System.Runtime.InteropServices;

const int HT_CAPTION = 0x2;
const int WM_NCLBUTTONDOWN = 0xA1;

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd,int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();


private void Form1_MouseDown(object sender, MouseEventArgs e)
{
  if (e.Button == MouseButtons.Left)
  {
    ReleaseCapture();
    SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
  }
}
Here Form1 is a border-less form and by using above co…