欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 无边框窗体移动代码

程序员文章站 2022-07-14 12:17:46
...

C# 无边框窗体移动代码

        Point _frmPoint = new Point(); //移动前窗体左上角坐标
        Point _mousePoint = new Point(); //按下鼠标时坐标
        bool move = false; //是否移动
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            _frmPoint = this.Location;
            _mousePoint.X = e.X;
            _mousePoint.Y = e.Y;
            move = true;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (!move)
                return;
            _frmPoint.X += -_mousePoint.X + e.X;
            _frmPoint.Y += -_mousePoint.Y+ e.Y;
            this.Location = _frmPoint;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {

            move = false;
        }

WPF 方式(winform下未测试)

  private void frmMain_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }