1. Description: In WPF, File download needs to display the download progress, because the system comes with a bar-type progress bar compared to occupy space, instead of using a circular progress bar, need to be presented on drawingvisual.
Effect of running
PrivatePoint Getpointoncir (Point CenterPoint,DoubleRDoubleAngel) {Point P =NewPoint (); p.x = Math.sin (Angel * Math.PI/ the) * r + centerpoint.x; P.Y = Centerpoint.y-math.cos (Angel * Math.PI/ the) * R;returnP }PrivateGeometry Drawingarc (Point bigcirclefirstpoint, point Bigcirclesecondpoint, point Smallcirclefirstpoint, point Smallcirclesecondpoint,DoubleBigcircleradius,DoubleSmallcircleradius,BOOLISLARGEARC) {PathFigure pathfigure =Newpathfigure {IsClosed =true}; Pathfigure.startpoint = Bigcirclefirstpoint; PATHFIGURE.SEGMENTS.ADD (Newarcsegment {point = Bigcirclesecondpoint, IsLargeArc = IsLargeArc, Size =NewSize (Bigcircleradius, Bigcircleradius), sweepdirection = Sweepdirection.clockwise }); PATHFIGURE.SEGMENTS.ADD (Newlinesegment {point = Smallcirclesecondpoint}); PATHFIGURE.SEGMENTS.ADD (Newarcsegment {point = Smallcirclefirstpoint, IsLargeArc = IsLargeArc, Size =NewSize (Smallcircleradius, Smallcircleradius), sweepdirection = Sweepdirection.counterclockwis e}); PathGeometry PathGeometry =NewPathGeometry (); PATHGEOMETRY.FIGURES.ADD (PathFigure);returnPathGeometry; }//calculate download Progress percentage based on saved size and total file size PrivateGeometry Getgeometry () {BOOLIsLargeArc =false;DoublePercent =Double. Parse (Convert.ToString (savedsize))/Double. Parse (Convert.ToString (fileSize)); Percentstring =string. Format ("{0}%", Math.Round (percent* -,0));DoubleAngel = percent * 360D;if(angel> the) islargearc=true;//double angel =; DoubleBIGR = -;DoubleSmallr = -; Point CenterPoint = VL. StartPoint;//new Point (at +);Point firstpoint = Getpointoncir (CenterPoint, BIGR,0); Point secondpoint = Getpointoncir (CenterPoint, BIGR, Angel); Point thirdpoint = Getpointoncir (CenterPoint, Smallr,0); Point fourpoint = Getpointoncir (CenterPoint, Smallr, Angel);returnDrawingarc (FIRSTP, Secondpoint, Thirdpoint, Fourpoint, Bigr, Smallr, IsLargeArc); }
Draw a circular progress bar, actually is the dynamic drawing two concentric circles, calculates the arc angle size according to the file saved percentage, needs 7 parameters: The circle radius bigr, the small circle radius smallr, the Concentric Circle Center CenterPoint, the Great circle starting point Firstpoint, The end point of the Great Circle Secondpoint, the starting point of the small circle Thirdpoint, the end point of the small circle Fourpoint
Finally, you need to use DrawingContext to draw the circle:
public Visual drawshape () { new drawingvisual (); DrawingContext DrawingContext = Drawingwordsvisual.renderopen (); try { if (savedsize! = fileSize) {
Drawingcontext.drawellipse (NULL,NewPen (Brushes.gray,3), VL. StartPoint, -, -);Drawingcontext.drawgeometry (vs. Visualbackgroundbrush, vs. Visualframepen, Getgeometry ());FormattedText formatwords =NewFormattedText (percentstring, System.Globalization.CultureInfo.CurrentCulture, Flowdirection.lefttoright,NewTypeface (vs. Wordsfont.name), vs. Wordsfont.size, Currentstyle.visualbackgroundbrush); Formatwords.setfontweight (Fontweights.bold); Point startPoint =NewPoint (VL. Startpoint.x-formatwords.width/2Vl. Startpoint.y-formatwords.height/2); Drawingcontext.drawtext (Formatwords, startPoint); }Else{Drawingcontext.drawellipse (NULL,NewPen (Brushes.green,3), VL. StartPoint, -, -); FormattedText formatwords =NewFormattedText ("Open", System.Globalization.CultureInfo.CurrentCulture, Flowdirection.lefttoright,NewTypeface (vs. Wordsfont.name), vs. Wordsfont.size, brushes.red); Formatwords.setfontweight (Fontweights.bold); Point startPoint =NewPoint (VL. Startpoint.x-formatwords.width/2Vl. Startpoint.y-formatwords.height/2); Drawingcontext.drawtext (Formatwords, startPoint); } }Catch(Exception ex) {NewSaveexceptioninfo (). Savelogastxtinfoex (ex. Message); }finally{Drawingcontext.close (); }returndrawingwordsvisual; }