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;
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播