编辑
2025-02-03
C# 应用
00
请注意,本文编写于 92 天前,最后修改于 92 天前,其中某些信息可能已经过时。

目录

摘要
正文

摘要

C#中的DrawArc方法可以用来绘制椭圆的一部分,通过指定一对坐标、宽度和高度,可以在屏幕上绘制出椭圆的部分弧线。该方法接受四个参数,分别是椭圆左上角和右下角的坐标,椭圆的宽度和高度。绘制完成后,可以通过设置相应的属性来控制画笔的颜色、线型、填充等效果。

正文

Graphics.DrawArc方法用于绘制表示由一对坐标,宽度和高度指定的椭圆的一部分的圆弧。此方法的重载列表中有4种方法,如下所示:

  • DrawArc(Pen,Rectangle,Single,Single)方法
  • DrawArc(Pen,RectangleF,Single,Single)方法
  • DrawArc(Pen,Int32,Int32,Int32,Int32,Int32,Int32)方法
  • DrawArc(Pen, Single, Single, Single, Single, Single, Single)方法

一个例子

绘制一个弧线,它是椭圆外围的一部分。 椭圆由矩形的边界定义。 弧线是参数与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); }

image.png

再来一个

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) { } } }

image.png

本文作者:rick

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!