How to draw a circular progressbar pie using GraphicsPath in WinForm?

前端 未结 1 1840
终归单人心
终归单人心 2021-01-14 03:10

I want my custom circular progress bar in WinForm. But the result doesnt fit to what I think. How can I draw the shape as the same in this picture?. I uploaded two image to

相关标签:
1条回答
  • 2021-01-14 04:09

    You can create a GraphicsPath, then add 2 arcs to the path using AddArc method:

    • Outer arc from start angle 270 and sweep angle 120 degree.
    • Inner arc in opposite direction, from start angle 270 + 120 and sweep angle -120 degree
    • Then close the path using GraphicsPath.CloseFigure.

    This way the you will have a thick arc as path.

    You can fill the path, using Graphics.FillPath method. And also you can draw the borders using GraphicsPath.DrawPath method.

    Result

    Code

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        var g = e.Graphics;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    
        var center = new Point(100, 100);
        var innerR = 30;
        var thickness = 20;
        var startAngle = 270;
        var arcLength = 120;
        var outerR = innerR + thickness;
        var outerRect = new Rectangle
                        (center.X - outerR, center.Y - outerR, 2 * outerR, 2 * outerR);
        var innerRect = new Rectangle
                        (center.X - innerR, center.Y - innerR, 2 * innerR, 2 * innerR);
    
        using (var p = new GraphicsPath())
        {
            p.AddArc(outerRect, startAngle, arcLength);
            p.AddArc(innerRect, startAngle + arcLength, -arcLength);
            p.CloseFigure();
            e.Graphics.FillPath(Brushes.Green, p);
            e.Graphics.DrawPath(Pens.Black, p);
        }
    }
    
    0 讨论(0)
提交回复
热议问题