C#代码展示:点击button按钮,在picturebox按住鼠标左键,拖动鼠标画矩形,松开鼠标左键矩形绘制完成,可重复这个操作画多个矩形,并且可以选中任意一个矩形拖动改变位置和改变大小,可以删除,并且根据窗体大小自适应
时间: 2023-08-12 08:23:23 浏览: 148
好的,下面是C#代码:
```csharp
public partial class Form1 : Form
{
private List<Rectangle> rectangles = new List<Rectangle>(); // 用于存储矩形
private Rectangle selectedRectangle; // 选中的矩形
private bool isDragging; // 是否正在拖动矩形
private Point mouseOffset; // 鼠标在矩形内的偏移量
public Form1()
{
InitializeComponent();
pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// 开始绘制矩形
Rectangle rect = new Rectangle(e.Location, new Size(0, 0));
rectangles.Add(rect);
selectedRectangle = rect;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (selectedRectangle != null)
{
// 拖动矩形
isDragging = true;
selectedRectangle.Width = e.Location.X - selectedRectangle.X;
selectedRectangle.Height = e.Location.Y - selectedRectangle.Y;
pictureBox1.Invalidate(); // 重绘pictureBox
}
}
else
{
// 鼠标未按下时,检查是否在矩形内
foreach (Rectangle rect in rectangles)
{
if (rect.Contains(e.Location))
{
selectedRectangle = rect;
mouseOffset = new Point(e.Location.X - rect.X, e.Location.Y - rect.Y);
pictureBox1.Cursor = Cursors.SizeAll; // 显示拖动光标
return;
}
}
selectedRectangle = null;
pictureBox1.Cursor = Cursors.Default; // 显示默认光标
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (isDragging)
{
// 结束拖动矩形
isDragging = false;
}
else
{
// 绘制矩形完成
selectedRectangle.Width = e.Location.X - selectedRectangle.X;
selectedRectangle.Height = e.Location.Y - selectedRectangle.Y;
pictureBox1.Invalidate(); // 重绘pictureBox
}
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// 绘制矩形
foreach (Rectangle rect in rectangles)
{
e.Graphics.DrawRectangle(Pens.Red, rect);
}
}
private void Form1_Resize(object sender, EventArgs e)
{
// pictureBox随窗体大小自适应
pictureBox1.Size = new Size(ClientSize.Width - pictureBox1.Location.X - 20,
ClientSize.Height - pictureBox1.Location.Y - 20);
}
}
```
同时,需要在窗体中添加一个PictureBox控件,并将其Dock属性设置为Fill,这样它就会随着窗体的大小自适应。另外,需要在pictureBox的Paint事件中调用Invalidate方法来重绘pictureBox。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)