using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace FiveScreen { public partial class FrmPainting1 : Form { bool isMouseDown = false; // 窗体是否移动 Point currentFormLocation = new Point(); // 当前窗体位置 Point currentMouseOffset = new Point(); // 当前鼠标的按下位置 public FrmPainting1() { InitializeComponent(); panel1.MouseDown += FrmInjection1_MouseDown; panel1.MouseMove += FrmInjection1_MouseMove; panel1.MouseUp += FrmInjection1_MouseUp; this.WindowState = FormWindowState.Maximized; } private void panel1_DoubleClick(object sender, EventArgs e) { //Application.Exit(); if (this.WindowState == FormWindowState.Maximized) { this.WindowState = FormWindowState.Normal; } else { this.WindowState = FormWindowState.Maximized; } } private void FrmInjection1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMouseDown = true; currentFormLocation = this.Location; currentMouseOffset = Control.MousePosition; } } private void FrmInjection1_MouseMove(object sender, MouseEventArgs e) { int rangeX = 0, rangeY = 0; // 计算当前鼠标光标的位移,让窗体进行相同大小的位移 if (isMouseDown) { Point pt = Control.MousePosition; rangeX = currentMouseOffset.X - pt.X; rangeY = currentMouseOffset.Y - pt.Y; this.Location = new Point(currentFormLocation.X - rangeX, currentFormLocation.Y - rangeY); } } private void FrmInjection1_MouseUp(object sender, MouseEventArgs e) { isMouseDown = false; // 停止移动 } } }