Monday 7 December 2015

[Solved] Move a windows form on mouse drag having no form border in C#

Given code will help you to make form draggable without a border.
For this you need to add mousedown event for any root control which you want to make draggable. It will change the windows form position based on mouse position on screen.


       
using System.Runtime.InteropServices;
#region dragform on mousedown
        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();
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }
 #endregion dragform on mousedown