Draw a circular progress bar on the drawingvisual, similar to the iOS system style.

Source: Internet
Author: User
Tags drawtext

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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.