C#中的DrawArc
方法可以用来绘制椭圆的一部分,通过指定一对坐标、宽度和高度,可以在屏幕上绘制出椭圆的部分弧线。该方法接受四个参数,分别是椭圆左上角和右下角的坐标,椭圆的宽度和高度。绘制完成后,可以通过设置相应的属性来控制画笔的颜色、线型、填充等效果。
Graphics.DrawArc方法用于绘制表示由一对坐标,宽度和高度指定的椭圆的一部分的圆弧。此方法的重载列表中有4种方法,如下所示:
一个例子
绘制一个弧线,它是椭圆外围的一部分。 椭圆由矩形的边界定义。 弧线是参数与startAngle
+ sweepAngle
参数之间的startAngle
椭圆外围部分。
C#protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Pen pen=new Pen(Color.DarkBlue, 2);
Rectangle rect=new Rectangle(0,0,200,200);
//定义开始 (45 度) 和扫描 (180 度) 角度。
float startAngle = 45.0F;
float sweepAngle = 180F;
e.Graphics.DrawArc(pen,rect,startAngle, sweepAngle);
}
再来一个
C#protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
for (int i = 1; i < 10; i++)
{
Pen pen = new Pen(Color.DarkBlue, 2);
Rectangle rect = new Rectangle(i * 5, i * 5, 200 - i * 10, 200 - i * 10);
float startAngle = 45.0F;
float sweepAngle = 270F;
e.Graphics.DrawArc(pen, rect, startAngle, sweepAngle);
}
}
加一个动画
C#public class CircleProgressBar : PictureBox
{
System.Timers.Timer timer;
public CircleProgressBar()
{
timer=new System.Timers.Timer();
timer.Interval = 10;
timer.Elapsed += Timer_Elapsed;
timer.Start();
}
float sweepAngle = 0F;
private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
if (sweepAngle <= 270)
{
sweepAngle++;
}
else
{
sweepAngle =0;
}
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
int width = this.Width;
int height = this.Height;
try
{
for (int i = 1; i < 5; i++)
{
Pen pen = new Pen(Color.DarkBlue, 2);
Rectangle rect = new Rectangle(i * 5, i * 5, width - i * 10, height - i * 10);
float startAngle = 45.0F;
e.Graphics.DrawArc(pen, rect, startAngle, sweepAngle);
}
}
catch(Exception ex)
{
}
}
}
本文作者:rick
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!