C bordered form form mobile implementation code

  • 2020-05-19 05:34:46
  • OfStack

Click anywhere on the form to move the form:

Need to add a namespace:

using System.Runtime.InteropServices;


private const int WM_NCLBUTTONDOWN = 0x00A1;
private  const int HTCAPTION = 2;
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern bool ReleaseCapture();
protected override void OnMouseDown( MouseEventArgs e )
{
    base.OnMouseDown( e );
    if (e.Button == MouseButtons.Left)  //  Press the left mouse button               
    {
        ReleaseCapture();   //  Release the capture                  
        SendMessage(this.Handle, WM_NCLBUTTONDOWN, (IntPtr)HTCAPTION, IntPtr.Zero);    //  Drag the form               
    }
}


Related articles: