Winform隐藏标题栏后实现窗口移动

88 阅读1分钟

备注:窗体上如果是有其他控件 ,或者是容器之类的在上面是拖不动,只能作用在窗体上,这点需要注意。。。

 #region 隐藏标题栏后移动窗口

        private bool m_isMouseDown = false;
        private Point m_mousePos = new Point();

        /// <summary>
        /// 鼠标按下,开启移动
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            m_mousePos = Cursor.Position;
            m_isMouseDown = true;
        }

        /// <summary>
        /// 鼠标抬起,关闭移动
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            m_isMouseDown = false;
            this.Focus();
        }

        /// <summary>
        /// 移动窗口
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (m_isMouseDown)
            {
                Point tempPos = Cursor.Position;
                this.Location = new Point(Location.X + (tempPos.X - m_mousePos.X), Location.Y + (tempPos.Y - m_mousePos.Y));
                m_mousePos = Cursor.Position;
            }
        }

        #endregion 隐藏标题栏后移动窗口