如何在C#中使用鼠标绘制和移动形状

我是C#编程的新手,想要求一些帮助。 我正在尝试使用鼠标左键移动我在Windows应用程序表单上绘制的颜色填充矩形,我正在尝试使用鼠标右键将其拖放到另一个位置。 目前我已设法绘制矩形,但右键单击是拖动整个表单。

这是我的代码:

public partial class Form1 : Form { private Point MouseDownLocation; public Form1() { InitializeComponent(); this.DoubleBuffered = true; } Rectangle rec = new Rectangle(0, 0, 0, 0); protected override void OnPaint(PaintEventArgs e) { e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec); } protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { rec = new Rectangle(eX, eY, 0, 0); Invalidate(); } if (e.Button == MouseButtons.Right) { MouseDownLocation = e.Location; } } protected override void OnMouseMove(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { rec.Width = eX - rec.X; rec.Height = eY - rec.Y; Invalidate(); } if (e.Button == MouseButtons.Right) { this.Left = eX + this.Left - MouseDownLocation.X; this.Top = eY + this.Top - MouseDownLocation.Y; } } } 

我只需要用鼠标右键拖放矩形。

编辑 :谢谢你,我得到了我的答案非常快,这里的代码是有效的:

 public partial class Form1 : Form { private Point MouseDownLocation; public Form1() { InitializeComponent(); this.DoubleBuffered = true; } Rectangle rec = new Rectangle(0, 0, 0, 0); protected override void OnPaint(PaintEventArgs e) { e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec); //Generates the shape } protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Left) //can also use this one: //if (e.Button == System.Windows.Forms.MouseButtons.Left) { rec = new Rectangle(eX, eY, 0, 0); Invalidate(); } if (e.Button == MouseButtons.Right) { MouseDownLocation = e.Location; } } protected override void OnMouseMove(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { rec.Width = eX - rec.X; rec.Height = eY - rec.Y; this.Invalidate(); } if (e.Button == MouseButtons.Right) { rec.Location = new Point((eX - MouseDownLocation.X) + rec.Left, (eY - MouseDownLocation.Y) + rec.Top); MouseDownLocation = e.Location; this.Invalidate(); } } } 

这似乎有效

  protected override void OnMouseMove(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { rec.Width = eX - rec.X; rec.Height = eY - rec.Y; this.Invalidate(); } if (e.Button == MouseButtons.Right) { rec.Location = new Point((eX-MouseDownLocation.X) + rec.Left, (eY-MouseDownLocation.Y) + rec.Top); MouseDownLocation = e.Location; this.Invalidate(); } } 

尝试这一个:请注意,我在此方法中向表单添加计时器,您不需要在鼠标事件上调用Invalidate timertick调用Refresh(),因此表单将在每个tick中绘制自己。

 public partial class Form1 : Form { private Point MouseDownLocation; public Form1() { InitializeComponent(); this.DoubleBuffered = true; timer1.Start(); // add timer to the form } Rectangle rec = new Rectangle(0, 0, 0, 0); protected override void OnPaint(PaintEventArgs e) { e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec); } private void timer1_Tick(object sender, EventArgs e) { Refresh(); } protected override void OnMouseMove(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { rec.Width = eX - rec.X; rec.Height = eY - rec.Y; } else if (e.Button == MouseButtons.Right) { rec.X = eX - MouseDownLocation.X; rec.Y = eY - MouseDownLocation.Y; } } protected override void OnMouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) MouseDownLocation = e.Location; } }