move flat form c# code example
Example 1: drag form in C# winform
[DllImport("user32")]
private static extern bool ReleaseCapture();
[DllImport("user32")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wp, int lp);
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, 161, 2, 0);
}
}
Example 2: c# make form draggable
private bool mouseDown;
private Point lastLocation;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
lastLocation = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(mouseDown)
{
this.Location = new Point(
(this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);
this.Update();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}