C#实现工具栏【画矩形】、【画多边形】和【画椭圆】的功能,即参照【画点】和【画线】工具栏的实现,为Click、MouseDown、MouseMove、MouseUp事件添加相应的代码完成鼠标绘图的功能。
时间: 2024-01-11 10:14:46 浏览: 121
这里提供一个简单的实现工具栏画矩形、画多边形和画椭圆的 C# 代码示例,你可以参考并根据自己的需求进行修改。
首先,我们需要在 Windows 窗体中添加一个工具栏控件,然后在工具栏中添加三个按钮,分别对应画矩形、画多边形和画椭圆的功能。代码如下:
```csharp
private void Form1_Load(object sender, EventArgs e)
{
// 添加工具栏控件
ToolStrip toolStrip = new ToolStrip();
this.Controls.Add(toolStrip);
// 添加“画矩形”按钮
ToolStripButton btnRect = new ToolStripButton();
btnRect.Text = "画矩形";
btnRect.Click += new EventHandler(btnRect_Click);
toolStrip.Items.Add(btnRect);
// 添加“画多边形”按钮
ToolStripButton btnPolygon = new ToolStripButton();
btnPolygon.Text = "画多边形";
btnPolygon.Click += new EventHandler(btnPolygon_Click);
toolStrip.Items.Add(btnPolygon);
// 添加“画椭圆”按钮
ToolStripButton btnEllipse = new ToolStripButton();
btnEllipse.Text = "画椭圆";
btnEllipse.Click += new EventHandler(btnEllipse_Click);
toolStrip.Items.Add(btnEllipse);
}
```
接着,我们需要在窗体中添加一个画板控件,用于显示绘制的图形。代码如下:
```csharp
private Bitmap bitmap; // 用于绘制的位图
private Graphics graphics; // 用于绘制的图形对象
private void Form1_Load(object sender, EventArgs e)
{
// ...
// 添加画板控件
PictureBox pictureBox = new PictureBox();
pictureBox.Dock = DockStyle.Fill;
pictureBox.BackColor = Color.White;
pictureBox.MouseDown += new MouseEventHandler(pictureBox_MouseDown);
pictureBox.MouseMove += new MouseEventHandler(pictureBox_MouseMove);
pictureBox.MouseUp += new MouseEventHandler(pictureBox_MouseUp);
this.Controls.Add(pictureBox);
// 创建位图和图形对象
bitmap = new Bitmap(pictureBox.ClientSize.Width, pictureBox.ClientSize.Height);
graphics = Graphics.FromImage(bitmap);
}
```
然后,我们需要实现三个按钮的点击事件,以及画板控件的鼠标事件。代码如下:
```csharp
private bool isDrawing = false; // 是否正在绘制
private Point startPt; // 绘制起点
private Point endPt; // 绘制终点
private List<Point> polygonPts = new List<Point>(); // 多边形的各个顶点坐标
private void btnRect_Click(object sender, EventArgs e)
{
// 点击“画矩形”按钮
isDrawing = true;
}
private void btnPolygon_Click(object sender, EventArgs e)
{
// 点击“画多边形”按钮
isDrawing = true;
}
private void btnEllipse_Click(object sender, EventArgs e)
{
// 点击“画椭圆”按钮
isDrawing = true;
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (isDrawing)
{
// 记录绘制起点
startPt = e.Location;
}
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
// 绘制中,记录绘制终点并重新绘制图形
endPt = e.Location;
DrawShape();
}
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
if (isDrawing)
{
// 绘制结束,记录绘制终点并重新绘制图形
endPt = e.Location;
DrawShape();
// 清除绘制状态和多边形顶点坐标
isDrawing = false;
polygonPts.Clear();
}
}
private void DrawShape()
{
// 先清空画板
graphics.Clear(Color.White);
if (btnRect.Checked)
{
// 绘制矩形
Rectangle rect = new Rectangle(startPt, new Size(endPt.X - startPt.X, endPt.Y - startPt.Y));
graphics.DrawRectangle(Pens.Black, rect);
}
else if (btnPolygon.Checked)
{
// 绘制多边形
polygonPts.Add(endPt);
if (polygonPts.Count > 1)
{
graphics.DrawPolygon(Pens.Black, polygonPts.ToArray());
}
}
else if (btnEllipse.Checked)
{
// 绘制椭圆
Rectangle rect = new Rectangle(startPt, new Size(endPt.X - startPt.X, endPt.Y - startPt.Y));
graphics.DrawEllipse(Pens.Black, rect);
}
// 在画板上显示绘制的图形
PictureBox pictureBox = (PictureBox)this.Controls[1];
pictureBox.Image = bitmap;
}
```
最后,我们需要在窗体关闭时释放资源。代码如下:
```csharp
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 释放资源
graphics.Dispose();
bitmap.Dispose();
}
```
至此,一个简单的工具栏画矩形、画多边形和画椭圆的 C# 实现就完成了。需要注意的是,这里只提供了一个基础的实现,如果需要更复杂的功能,还需要进一步完善代码。
阅读全文