C#winform拖动无边框窗体

 private bool isMouseLeftKeyDown = false;
        private Point mousePointToClient = new Point();//相对于本窗体鼠标位置
        private Point mousePointToScreen = new Point();//相对于屏幕鼠标位置

        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
Control ctrl = sender as Control; isMouseLeftKeyDown = true; this.mousePointToClient = new Point(e.X + ctrl.Location.X, e.Y + ctrl.Location.Y); this.mousePointToScreen = Control.MousePosition; } } private void Form_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && isMouseLeftKeyDown) { this.Top = Control.MousePosition.Y - mousePointToClient.Y; this.Left = Control.MousePosition.X - mousePointToClient.X; } } private void Form_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMouseLeftKeyDown = false; FormClick(); } } private void FormClick() { //鼠标位置没有发成偏移,视为点击事件 if (mousePointToScreen != Control.MousePosition) return; //todo }

  

posted @ 2019-04-09 15:16  翻白眼的哈士奇  阅读(159)  评论(0编辑  收藏  举报