C#在picturebox中绘制1,2,3,4,5秒,对应半径为56,59,63,66,70的动态圆
时间: 2024-06-13 19:07:38 浏览: 68
以下是C#在PictureBox中绘制1,2,3,4,5秒,对应半径为56,59,63,66,70的动态圆的代码:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DynamicCircle
{
public partial class Form1 : Form
{
private int radius = 56; // 初始半径
private int time = 1; // 初始时间
private Timer timer = new Timer(); // 定时器
public Form1()
{
InitializeComponent();
timer.Interval = 1000; // 定时器间隔为1秒
timer.Tick += new EventHandler(timer_Tick); // 绑定定时器事件
timer.Start(); // 启动定时器
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black, 2); // 画笔颜色为黑色,宽度为2
int x = pictureBox1.Width / 2; // 圆心横坐标
int y = pictureBox1.Height / 2; // 圆心纵坐标
g.DrawEllipse(pen, x - radius, y - radius, 2 * radius, 2 * radius); // 绘制圆形
}
private void timer_Tick(object sender, EventArgs e)
{
time++; // 时间加1
if (time > 5) // 时间大于5秒时停止定时器
{
timer.Stop();
return;
}
switch (time) // 根据时间设置半径
{
case 2:
radius = 59;
break;
case 3:
radius = 63;
break;
case 4:
radius = 66;
break;
case 5:
radius = 70;
break;
}
pictureBox1.Refresh(); // 刷新PictureBox
}
}
}
```
阅读全文