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 }