【问题标题】:How to draw a circular progressbar pie using GraphicsPath in WinForm?如何在 WinForm 中使用 GraphicsPath 绘制圆形进度条饼图?
【发布时间】:2019-10-28 02:51:36
【问题描述】:

我想在 WinForm 中自定义圆形进度条。但结果不符合我的想法。我怎样才能在这张图片中绘制相同的形状?我上传了两张图片以明确我的问题。

我的代码:

void Form1_Paint(object sender, PaintEventArgs e)
    {
        int angle = 120;

        e.Graphics.SmoothingMode = SmoothingMode.HighQuality;

        Rectangle outerRect = new Rectangle(50, 50, 100, 100);
        Rectangle innerRect = new Rectangle(70, 70, 60, 60);

        int innerRadius = innerRect.Width / 2;
        int outerRadius = outerRect.Width / 2;

        Point innerCenter = new Point(innerRect.X + innerRadius, innerRect.Y + innerRadius);
        Point outerCenter = new Point(outerRect.X + outerRadius, outerRect.Y + outerRadius);

        GraphicsPath outerCircle = new GraphicsPath();
        outerCircle.AddEllipse(outerRect);

        GraphicsPath innerCircle = new GraphicsPath();
        innerCircle.AddEllipse(innerRect);

        GraphicsPath progPath = new GraphicsPath();

        Point p1 = new Point(outerRect.X + outerRadius, outerRect.Y);
        Point p2 = new Point(innerRect.X + innerRadius, innerRect.Y);


        Point inner = new Point((int)(innerRadius * Math.Cos(angle * Math.PI / 180) + innerCenter.X),
                                (int)(innerRadius * Math.Sin(angle * Math.PI / 180) + innerCenter.Y));
        Point outer = new Point((int)(outerRadius * Math.Cos(angle * Math.PI / 180) + outerCenter.X),
                                (int)(outerRadius * Math.Sin(angle * Math.PI / 180) + outerCenter.Y));

        progPath.AddLine(p1, p2);
        progPath.AddArc(innerRect, -90, angle);
        progPath.AddLine(inner, outer);
        progPath.AddArc(outerRect, angle - 90,-angle);

        progPath.Widen(Pens.Black);
        e.Graphics.DrawPath(Pens.Black, progPath);

    }

【问题讨论】:

    标签: c# winforms custom-controls gdi+


    【解决方案1】:

    您可以创建一个GraphicsPath,然后使用AddArc 方法在路径中添加2个弧:

    • 起始角270 和扫掠角120 度的外圆弧。
    • 反方向的内圆弧,从起始角270 + 120和扫角-120度数
    • 然后使用GraphicsPath.CloseFigure关闭路径。

    这样你会有一个粗弧作为路径。

    您可以使用Graphics.FillPath 方法填充路径。您也可以使用GraphicsPath.DrawPath 方法绘制边框。

    结果

    代码

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

    【讨论】:

    • 谢谢@Reza。这种方法比我的方法简单。
    猜你喜欢
    • 1970-01-01
    • 2014-07-15
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 2022-08-06
    • 2021-10-15
    相关资源
    最近更新 更多