WinForm 自定义窗体的拖动方法

来源:互联网 发布:中国国民收入数据库 编辑:程序博客网 时间:2024/04/30 14:12

使用C#开发WinForm应用程序,自定义的窗体无法自动移动。需要调用Windows API,以实现Form窗口的灵活移动。实现代码如下:

        #region  控制窗体拖动        [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>        /// 通过Windows的API控制窗体的拖动        /// </summary>        /// <param name="hwnd"></param>        public static void MouseDown(IntPtr hwnd)        {            ReleaseCapture();            SendMessage(hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);        }        #endregion

在Form的MouseDown事件中,执行鼠标按下拖动窗体操作,代码如下:

private void Form_MouseDown(object sender, MouseEventArgs e)        {            MouseDown(this.Handle);        }


0 0
原创粉丝点击