c# 实现圆形的进度条(ProgressBar)

  public class ValueToProcessConverter : IValueConverter

  {

  readonly double Thickness = 20;

  private Point centerPoint;

  private double radius;

  readonly SolidColorBrush NormalBrush = new SolidColorBrush(Colors.White);

  readonly SolidColorBrush EllipseBrush = new SolidColorBrush(Color.FromRgb(107, 132, 165));

  string percentString;

  private static readonly Typeface SuccessRateTypeface;

  private const int SuccessRateFontSize = 65;

  readonly double SuccessRateFontCorrectionValue = 12;

  static ValueToProcessConverter()

  {

  SuccessRateTypeface = new Typeface(new FontFamily("MSYH"), new FontStyle(), new FontWeight(), new FontStretch());

  }

  public ValueToProcessConverter()

  {

  }

  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

  {

  if (value is double && !string.IsNullOrEmpty((string)parameter))

  {

  double arg = (double)value;

  double width = double.Parse((string)parameter);

  radius = width / 2;

  centerPoint = new Point(radius, radius);

  return DrawBrush(arg, 100, radius, radius, Thickness);

  }

  else

  {

  throw new ArgumentException();

  }

  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

  {

  throw new NotImplementedException();

  }

  ///

  /// 根据角度获取坐标

  ///

  ///

  ///

  ///

  ///

  private Point GetPointByAngel(Point CenterPoint, double r, double angel)

  {

  Point p = new Point();

  p.X = Math.Sin(angel * Math.PI / 180) * r + CenterPoint.X;

  p.Y = CenterPoint.Y - Math.Cos(angel * Math.PI / 180) * r;

  return p;

  }

  ///

  /// 根据4个坐标画出扇形

  ///

  ///

  ///

  ///

  ///

  ///

  ///

  ///

  ///

  private Geometry DrawingArcGeometry(Point bigFirstPoint, Point bigSecondPoint, Point smallFirstPoint, Point smallSecondPoint, double bigRadius, double smallRadius, bool isLargeArc)

  {

  PathFigure pathFigure = new PathFigure { IsClosed = true };

  pathFigure.StartPoint = bigFirstPoint;

  pathFigure.Segments.Add(

  new ArcSegment

  {

  Point = bigSecondPoint,

  IsLargeArc = isLargeArc,

  Size = new Size(bigRadius, bigRadius),

  SweepDirection = SweepDirection.Clockwise

  });

  pathFigure.Segments.Add(new LineSegment { Point = smallSecondPoint });

  pathFigure.Segments.Add(

  new ArcSegment

  {

  Point = smallFirstPoint,

  IsLargeArc = isLargeArc,

  Size = new Size(smallRadius, smallRadius),

  SweepDirection = SweepDirection.Counterclockwise

  });

  PathGeometry pathGeometry = new PathGeometry();

  pathGeometry.Figures.Add(pathFigure);

  return pathGeometry;

  }

  ///

  /// 根据当前值和最大值获取扇形

  ///

  ///

  ///

  ///

  private Geometry GetGeometry(double value, double maxValue, double radiusX, double radiusY, double thickness)

  {

  bool isLargeArc = false;

  double percent = value / maxValue;

  percentString = string.Format("{0}%", Math.Round(percent * 100, 2));

  double angel = percent * 360D;

  if (angel > 180) isLargeArc = true;

  double bigR = radiusX + thickness / 2;

  double smallR = radiusX - thickness / 2;

  Point firstpoint = GetPointByAngel(centerPoint, bigR, 0);

  Point secondpoint = GetPointByAngel(centerPoint, bigR, angel);

  Point thirdpoint = GetPointByAngel(centerPoint, smallR, 0);

  Point fourpoint = GetPointByAngel(centerPoint, smallR, angel);

  return DrawingArcGeometry(firstpoint, secondpoint, thirdpoint, fourpoint, bigR, smallR, isLargeArc);

  }

  ///

  /// 画扇形

  ///

  ///

  ///

  ///

  ///

  ///

  ///

  private void DrawingGeometry(DrawingContext drawingContext, double value, double maxValue, double radiusX, double radiusY, double thickness)

  {

  drawingContext.DrawEllipse(null, new Pen(EllipseBrush, thickness), centerPoint, radiusX, radiusY);

  drawingContext.DrawGeometry(NormalBrush, new Pen(), GetGeometry(value, maxValue, radiusX, radiusY, thickness));

  FormattedText formatWords = new FormattedText(percentString, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, SuccessRateTypeface, SuccessRateFontSize, NormalBrush);

  Point startPoint = new Point(centerPoint.X - formatWords.Width / 2, centerPoint.Y - formatWords.Height / 2 - SuccessRateFontCorrectionValue);

  drawingContext.DrawText(formatWords, startPoint);

  drawingContext.Close();

  }

  ///

  /// 根据当前值和最大值画出进度条

  ///

  ///

  ///

  ///

  private Brush DrawBrush(double value, double maxValue, double radiusX, double radiusY, double thickness)

  {

  DrawingGroup drawingGroup = new DrawingGroup();

  DrawingContext drawingContext = drawingGroup.Open();

  DrawingGeometry(drawingContext, value, maxValue, radiusX, radiusY, thickness);

  DrawingBrush brush = new DrawingBrush(drawingGroup);

  return brush;

  }

  }