代码展示:C#点击button后在 picturebox上按住鼠标左键拖动鼠标画矩形,松开鼠标左键矩形绘制完成,可以画多个矩形,并且可以选中矩形拖动位置和改变大小,并将矩形点位放在datagridview控件里,可以画多个矩形放在集合里序列化保存和反序列化加载
时间: 2024-02-26 12:58:45 浏览: 151
用c#实现画图功能,可以画出各种图形,如矩形,圆形,菱形等,还可以随意拖动,放大缩小
4星 · 用户满意度95%
以下是一个实现上述功能的C#代码示例:
```csharp
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace DrawRectangles
{
public partial class MainForm : Form
{
private bool isDrawing = false;
private bool isSelecting = false;
private Point startPoint;
private Point endPoint;
private List<Rectangle> rectangles = new List<Rectangle>();
private int selectedRectangleIndex = -1;
public MainForm()
{
InitializeComponent();
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "Rectangles", HeaderText = "Rectangles" });
dataGridView1.SelectionChanged += DataGridView1_SelectionChanged;
dataGridView1.CellEndEdit += DataGridView1_CellEndEdit;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (!isSelecting)
{
startPoint = e.Location;
isDrawing = true;
}
else
{
for (int i = 0; i < rectangles.Count; i++)
{
if (rectangles[i].Contains(e.Location))
{
selectedRectangleIndex = i;
break;
}
}
}
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
endPoint = e.Location;
pictureBox1.Invalidate();
}
else if (isSelecting && selectedRectangleIndex != -1)
{
int width = rectangles[selectedRectangleIndex].Width;
int height = rectangles[selectedRectangleIndex].Height;
int x = e.X - startPoint.X + rectangles[selectedRectangleIndex].X;
int y = e.Y - startPoint.Y + rectangles[selectedRectangleIndex].Y;
rectangles[selectedRectangleIndex] = new Rectangle(x, y, width, height);
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (isDrawing)
{
endPoint = e.Location;
isDrawing = false;
rectangles.Add(new Rectangle(Math.Min(startPoint.X, endPoint.X), Math.Min(startPoint.Y, endPoint.Y), Math.Abs(startPoint.X - endPoint.X), Math.Abs(startPoint.Y - endPoint.Y)));
dataGridView1.Rows.Add(new object[] { rectangles[rectangles.Count - 1].ToString() });
pictureBox1.Invalidate();
}
else if (isSelecting)
{
selectedRectangleIndex = -1;
}
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (Rectangle rectangle in rectangles)
{
if (rectangle == rectangles[selectedRectangleIndex])
{
e.Graphics.DrawRectangle(Pens.Red, rectangle);
}
else
{
e.Graphics.DrawRectangle(Pens.Black, rectangle);
}
}
if (isDrawing)
{
e.Graphics.DrawRectangle(Pens.Blue, Math.Min(startPoint.X, endPoint.X), Math.Min(startPoint.Y, endPoint.Y), Math.Abs(startPoint.X - endPoint.X), Math.Abs(startPoint.Y - endPoint.Y));
}
}
private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
selectedRectangleIndex = dataGridView1.SelectedRows[0].Index;
pictureBox1.Invalidate();
}
}
private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
string rectangleString = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
Rectangle rectangle = Rectangle.FromLTRB(int.Parse(rectangleString.Split(',')[0]), int.Parse(rectangleString.Split(',')[1]), int.Parse(rectangleString.Split(',')[2]), int.Parse(rectangleString.Split(',')[3]));
rectangles[e.RowIndex] = rectangle;
pictureBox1.Invalidate();
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Rectangles File (*.rect)|*.rect";
if (dialog.ShowDialog() == DialogResult.OK)
{
using (FileStream fileStream = new FileStream(dialog.FileName, FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fileStream, rectangles);
}
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Rectangles File (*.rect)|*.rect";
if (dialog.ShowDialog() == DialogResult.OK)
{
using (FileStream fileStream = new FileStream(dialog.FileName, FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
rectangles = (List<Rectangle>)formatter.Deserialize(fileStream);
}
dataGridView1.Rows.Clear();
foreach (Rectangle rectangle in rectangles)
{
dataGridView1.Rows.Add(new object[] { rectangle.ToString() });
}
pictureBox1.Invalidate();
}
}
}
}
```
在窗体设计器中添加一个PictureBox控件和一个DataGridView控件,并将pictureBox1的MouseDown、MouseMove、MouseUp和Paint事件分别绑定到pictureBox1_MouseDown、pictureBox1_MouseMove、pictureBox1_MouseUp和pictureBox1_Paint方法。将dataGridView1的SelectionChanged和CellEndEdit事件分别绑定到DataGridView1_SelectionChanged和DataGridView1_CellEndEdit方法。添加一个名为btnSave的Button控件和一个名为btnLoad的Button控件,并将它们的Click事件分别绑定到btnSave_Click和btnLoad_Click方法。
在以上示例中,我们使用了一个名为rectangles的List<Rectangle>集合来保存所有绘制的矩形。在pictureBox1的MouseDown、MouseMove和MouseUp事件中,我们分别实现了绘制矩形和选中矩形并拖动和改变大小的功能。在dataGridView1的SelectionChanged事件中,我们实现了选中一行时将对应的矩形设置为选中状态的功能。在dataGridView1的CellEndEdit事件中,我们实现了更新矩形集合中对应的矩形的坐标和大小的功能。在btnSave_Click和btnLoad_Click事件中,我们使用序列化和反序列化来保存和加载矩形集合。
阅读全文