c#实现圆形的进度条(ProgressBar)
在我们实际的⼯作中可能经常使⽤到圆形的进度条,但是这是怎么实现的呢?其实这只不过是修改了⼀下ProgressBar的模板,我们在下⾯的代码中我们将ProgressBar的Value值绑定到Border的Background上⾯,并且使⽤了⼀个ValueToProcessConverter的转换器进⾏相应地转换,这⾥重点介绍⼀下这个转换器
<ProgressBar Name="pb" Minimum="0" Maximum="100" >
<ProgressBar.Template>
<ControlTemplate TargetType="ProgressBar">
<Border Background="{TemplateBinding Value, Converter={StaticResource ValueToProcessConverter}, ConverterParameter=250}"/>
</ControlTemplate>
</ProgressBar.Template>
</ProgressBar>
下⾯介绍这部分的源码,并做简要的分析:
⾸先,获取ProgressBar.Value,然后再获取ConverterParameter=250这个值,通过这两个值就能确定画的圆环的⼤⼩和ProgressBar显⽰的值,然后我们再调⽤DrawBrush(arg, 100, radius, radius, Thickness)这个函数来进⾏绘制,具体代码如下:
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;
}
这⾥需要注意的是绝不能直接实例化 DrawingContext;但可以通过某些⽅法(例如 DrawingGroup.Open 和 DrawingVisual.RenderOpen)获取绘图上下⽂。我们这⾥是使⽤DrawingGroup.Open的⽅法来进⾏相应的绘图,然后在⾥⾯调⽤⾥DrawingGeometry这个函数,在这个函数中开始绘制⼀些DrawEllipse和DrawGeometry,在这个函数中我们讲解⼀下FormattedText 这个类,使⽤ FormattedText 对象可以绘制多⾏⽂本,且可以单独对该⽂本中的每个字符设置格式。
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();
}
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()
{param name
}
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();
}
/// <summary>
/// 根据⾓度获取坐标
/// </summary>
/// <param name="CenterPoint"></param>
/// <param name="r"></param>
/// <param name="angel"></param>
/// <returns></returns>
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;
}
/
// <summary>
/// 根据4个坐标画出扇形
/// </summary>
/// <param name="bigFirstPoint"></param>
/// <param name="bigSecondPoint"></param>
/// <param name="smallFirstPoint"></param>
/// <param name="smallSecondPoint"></param>
/// <param name="bigRadius"></param>
/// <param name="smallRadius"></param>
/// <param name="isLargeArc"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 根据当前值和最⼤值获取扇形
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 画扇形
/// </summary>
/// <param name="drawingContext"></param>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <param name="radiusX"></param>
/// <param name="radiusY"></param>
/// <param name="thickness"></param>
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();
}
/// <summary>
/// 根据当前值和最⼤值画出进度条
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <returns></returns>
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;
}
}
以上就是c# 实现圆形的进度条(ProgressBar)的详细内容,更多关于c# 实现进度条的资料请关注其它相关⽂章!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论