在DrawingVisual上绘制圆形的进度条,类似于IOS系统风格。
生活随笔
收集整理的這篇文章主要介紹了
在DrawingVisual上绘制圆形的进度条,类似于IOS系统风格。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.說明:在WPF中,文件下載時需要顯示下載進度,由于系統自帶的條型進度條比較占用空間,改用圓形的進度條,需要在DrawingVisual上呈現。
運行的效果如圖:
private Point GetPointOnCir(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;}private Geometry drawingArc(Point bigCirclefirstPoint, Point bigCirclesecondPoint, Point smallCirclefirstPoint, Point smallCirclesecondPoint, double bigCircleRadius, double smallCircleRadius,bool isLargeArc){PathFigure pathFigure = new PathFigure { IsClosed = true };pathFigure.StartPoint = bigCirclefirstPoint;pathFigure.Segments.Add(new ArcSegment{Point = bigCirclesecondPoint,IsLargeArc = isLargeArc,Size = new Size(bigCircleRadius, bigCircleRadius),SweepDirection = SweepDirection.Clockwise});pathFigure.Segments.Add(new LineSegment { Point = smallCirclesecondPoint });pathFigure.Segments.Add(new ArcSegment{Point = smallCirclefirstPoint,IsLargeArc = isLargeArc,Size = new Size(smallCircleRadius, smallCircleRadius),SweepDirection = SweepDirection.Counterclockwise});PathGeometry pathGeometry = new PathGeometry();pathGeometry.Figures.Add(pathFigure);return pathGeometry;}//根據已保存的大小和文件總大小來計算下載進度百分比private Geometry GetGeometry(){bool isLargeArc =false;double percent = double.Parse(Convert.ToString(savedSize)) / double.Parse(Convert.ToString(fileSize));PercentString = string.Format("{0}%",Math.Round(percent*100,0));double angel = percent * 360D;if(angel>180)isLargeArc=true;//double angel = 45;double bigR = 16;double smallR = 13;Point centerPoint = vl.StartPoint;//new Point(100, 300);Point firstpoint = GetPointOnCir(centerPoint, bigR, 0);Point secondpoint = GetPointOnCir(centerPoint, bigR, angel);Point thirdpoint = GetPointOnCir(centerPoint, smallR, 0);Point fourpoint = GetPointOnCir(centerPoint, smallR, angel);return drawingArc(firstp, secondpoint, thirdpoint, fourpoint, bigR, smallR, isLargeArc);}?
畫圓形的進度條,實際上是動態畫兩個同心圓,根據文件保存的百分比來計算畫弧形的角度的大小,需要7個參數:大圓的半徑bigR 、小圓的半徑smallR 、同心圓的圓心centerPoint 、大圓的起始點firstpoint 、大圓的結束點secondpoint 、小圓的起始點thirdpoint、小圓的結束點fourpoint
?
?
最后需要使用DrawingContext把圓給畫出來:
public Visual drawShape(){DrawingVisual drawingWordsVisual = new DrawingVisual();DrawingContext drawingContext = drawingWordsVisual.RenderOpen();try{if (savedSize != fileSize){ drawingContext.DrawEllipse(null, new Pen(Brushes.Gray, 3), vl.StartPoint, 13, 13);drawingContext.DrawGeometry(vs.VisualBackgroundBrush, vs.VisualFramePen, GetGeometry());FormattedText formatWords = new FormattedText(PercentString, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(vs.WordsFont.Name), vs.WordsFont.Size, currentStyle.VisualBackgroundBrush);formatWords.SetFontWeight(FontWeights.Bold);Point startPoint = new Point(vl.StartPoint.X - formatWords.Width / 2, vl.StartPoint.Y - formatWords.Height / 2);drawingContext.DrawText(formatWords, startPoint);}else{drawingContext.DrawEllipse(null, new Pen(Brushes.Green, 3), vl.StartPoint, 16, 16);FormattedText formatWords = new FormattedText("Open", System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(vs.WordsFont.Name), vs.WordsFont.Size, Brushes.Red);formatWords.SetFontWeight(FontWeights.Bold);Point startPoint = new Point(vl.StartPoint.X - formatWords.Width / 2, vl.StartPoint.Y - formatWords.Height / 2);drawingContext.DrawText(formatWords, startPoint);}}catch (Exception ex){new SaveExceptionInfo().SaveLogAsTXTInfoex(ex.Message);}finally{drawingContext.Close();}return drawingWordsVisual;}轉載于:https://www.cnblogs.com/goxmpx/p/3741915.html
總結
以上是生活随笔為你收集整理的在DrawingVisual上绘制圆形的进度条,类似于IOS系统风格。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HFSS天线设计流程
- 下一篇: GHOST重装系统后分区丢失只剩C盘的数