如何在WinForm中使用GraphicsPath绘制圆形进度条饼?

我想在WinForm中使用自定义循环进度条。 但结果不符合我的想法。 如何在这张图片中绘制相同的形状? 我上传了两张图片,以清楚我的问题。

目标形状

我的代码结果

我的代码是这样做的:

void Form1_Paint(object sender, PaintEventArgs e) { int angle = 120; e.Graphics.SmoothingMode = SmoothingMode.HighQuality; Rectangle outerRect = new Rectangle(50, 50, 100, 100); Rectangle innerRect = new Rectangle(70, 70, 60, 60); int innerRadius = innerRect.Width / 2; int outerRadius = outerRect.Width / 2; Point innerCenter = new Point(innerRect.X + innerRadius, innerRect.Y + innerRadius); Point outerCenter = new Point(outerRect.X + outerRadius, outerRect.Y + outerRadius); GraphicsPath outerCircle = new GraphicsPath(); outerCircle.AddEllipse(outerRect); GraphicsPath innerCircle = new GraphicsPath(); innerCircle.AddEllipse(innerRect); GraphicsPath progPath = new GraphicsPath(); Point p1 = new Point(outerRect.X + outerRadius, outerRect.Y); Point p2 = new Point(innerRect.X + innerRadius, innerRect.Y); Point inner = new Point((int)(innerRadius * Math.Cos(angle * Math.PI / 180) + innerCenter.X), (int)(innerRadius * Math.Sin(angle * Math.PI / 180) + innerCenter.Y)); Point outer = new Point((int)(outerRadius * Math.Cos(angle * Math.PI / 180) + outerCenter.X), (int)(outerRadius * Math.Sin(angle * Math.PI / 180) + outerCenter.Y)); progPath.AddLine(p1, p2); progPath.AddArc(innerRect, -90, angle); progPath.AddLine(inner, outer); progPath.AddArc(outerRect, angle - 90,-angle); progPath.Widen(Pens.Black); e.Graphics.DrawPath(Pens.Black, progPath); } 

您可以创建GraphicsPath ,然后使用AddArc方法将2个弧添加到路径:

  • 外弧从起始角度270和扫掠角度120度。
  • 内弧相反方向,从起始角度270 + 120和扫掠角度-120
  • 然后使用GraphicsPath.CloseFigure关闭路径。

这样你就会有一条粗弧作为路径。

您可以使用Graphics.FillPath方法填充路径。 您还可以使用GraphicsPath.drawPath方法绘制边框。

结果

在此处输入图像描述

 private void Form1_Paint(object sender, PaintEventArgs e) { var g = e.Graphics; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; var center = new Point(100, 100); var innerR = 30; var thickness = 20; var startAngle = 270; var arcLength = 120; var outerR = innerR + thickness; var outerRect = new Rectangle (center.X - outerR, center.Y - outerR, 2 * outerR, 2 * outerR); var innerRect = new Rectangle (center.X - innerR, center.Y - innerR, 2 * innerR, 2 * innerR); using (var p = new GraphicsPath()) { p.AddArc(outerRect, startAngle, arcLength); p.AddArc(innerRect, startAngle + arcLength, -arcLength); p.CloseFigure(); e.Graphics.FillPath(Brushes.Green, p); e.Graphics.DrawPath(Pens.Black, p); } }