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(); } }