WinForm无边框移动几种方法整理

 #region 【WinForm无边框窗体移动方法1】

        private readonly uint HTCAPTION = 2;
        private readonly uint SC_MOVE = 0xF010;
        private readonly uint WM_SYSCOMMAND = 0x112;

        [DllImport("user32.dll")]
        private static extern bool ReleaseCapture();

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, uint wParam, uint lParam);


        private void FrmLogin_Load(object sender, EventArgs e)
        {
            MouseDown += MyMouseMove;
            foreach (Control c in Controls) c.MouseDown += MyMouseMove;
        }

        private void MyMouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
        }

        #endregion
       #region 【WinForm无边框窗体移动方法2】

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();

        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        /// <summary>
        ///     窗体的MouseDown事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        /// <summary>
        ///     窗体加载时候绑定MouseDown事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            MouseDown += FrmMain_MouseDown;
        }

        #endregion
       #region 【WinForm无边框窗体移动方法3】

        private const int HTLEFT = 10;
        private const int HTRIGHT = 11;
        private const int HTTOP = 12;
        private const int HTTOPLEFT = 13;
        private const int HTTOPRIGHT = 14;
        private const int HTBOTTOM = 15;
        private const int HTBOTTOMLEFT = 0x10;
        private const int HTBOTTOMRIGHT = 17;

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x0084:
                    base.WndProc(ref m);
                    var vPoint = new Point((int) m.LParam & 0xFFFF, ((int) m.LParam >> 16) & 0xFFFF);
                    vPoint = PointToClient(vPoint);

                    if (vPoint.X <= 5)
                    {
                        if (vPoint.Y <= 5)
                            m.Result = (IntPtr) HTTOPLEFT;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr) HTBOTTOMLEFT;
                        else
                            m.Result = (IntPtr) HTLEFT;
                    }
                    else if (vPoint.X >= ClientSize.Width - 5)
                    {
                        if (vPoint.Y <= 5)
                            m.Result = (IntPtr) HTTOPRIGHT;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr) HTBOTTOMRIGHT;
                        else
                            m.Result = (IntPtr) HTRIGHT;
                    }
                    else if (vPoint.Y <= 5)
                    {
                        m.Result = (IntPtr) HTTOP;
                    }
                    else if (vPoint.Y >= ClientSize.Height - 5)
                    {
                        m.Result = (IntPtr) HTBOTTOM;
                    }

                    break;

                case 0x0201: //鼠标左键按下的消息 
                    m.Msg = 0x00A1; //更改消息为非客户区按下鼠标 
                    m.LParam = IntPtr.Zero; //默认值 
                    m.WParam = new IntPtr(2); //鼠标放在标题栏内 
                    base.WndProc(ref m);
                    break;

                default:
                    base.WndProc(ref m);
                    break;
            }
        }

        #endregion
      #region 【WinForm无边框窗体移动方法4】

        /// <summary>
        ///     重写WndProc方法,实现窗体移动和禁止双击最大化
        /// </summary>
        /// <param name="m">Windows 消息</param>
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x4e:
                case 0xd:
                case 0xe:
                case 0x14:
                    base.WndProc(ref m);
                    break;

                case 0x84: //鼠标点任意位置后可以拖动窗体
                    DefWndProc(ref m);
                    if (m.Result.ToInt32() == 0x01) m.Result = new IntPtr(0x02);
                    break;

                case 0xA3: //禁止双击最大化
                    break;

                default:
                    base.WndProc(ref m);
                    break;
            }
        }

        #endregion
原文地址:https://www.cnblogs.com/LifeDecidesHappiness/p/14565351.html