Winform 没有FormBorder的情况下 支持鼠标移动窗体

 由于windows自带的系统窗体风格不一定满足不同用户的需求,所以有些form可能需要自定义外观  比如自定义最小化,最大化,关闭等这类图标及显示效果等,这种情况下我们需要设置FormBorderStyle为None,但没有border的窗体,没法支持鼠标拖动,以下给出两种方案供参考:

 

#region Support to move the form
        // Solution 1
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParan);
        public const int HTCAPTION = 2;
        public const int WM_SYSCOMMAND = 0x112;
        public const int SC_MOVE = 0xf012;

        private void frmTarget_MouseMove(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(base.Handle, WM_SYSCOMMAND, SC_MOVE, 0);
        }

        // Solution 2
        //private Point mouseOffset;
        //private void frmTarget_MouseDown(object sender, MouseEventArgs e)
        //{
        //    mouseOffset = new Point(-e.X, -e.Y);
        //}

        //private void frmTarget_MouseMove(object sender, MouseEventArgs e)
        //{
        //    if (Control.MouseButtons == MouseButtons.Left)
        //    {
        //        Point mouseCoordinate = new Point(MousePosition.X, MousePosition.Y);
        //        mouseCoordinate.Offset(mouseOffset);
        //        this.Location = mouseCoordinate;
        //    }
        //}
        #endregion


展开阅读全文

没有更多推荐了,返回首页