DrawArc 即将结束

DrawArc is Coming up Short

我在使用 Graphics.DrawArc 方法时遇到了一个小问题。使用时会比实际尺寸短。我将此控件基于另一个 post found here

我正在尝试将其变成具有某些属性的 UserControl 并对其进行扩展。问题是当我将百分比设置为 50% 时,它出现了不足...

这是 UserControl 在 50% 时的样子...它应该居中(蓝色)在圆圈的底部。我已尝试尽我所能进行调整,但我现在迷路了。

这是我当前的代码...

Color _ProgressCompletedColor = SystemColors.MenuHighlight;
    Color _ProgressNotCompleted = Color.LightGray;
    Int32 _ProgressThickness = 2;
    Single _ProgressCompleted = 25;

    public AttuneProgressBar()
    {
        InitializeComponent();
    }

    public Single PercentageCompleted
    {
        get
        {
            return this._ProgressCompleted;
        }
        set
        {
            this._ProgressCompleted = value;
            this.Invalidate();
        }
    }

    public Int32 ProgressBarThickness
    {
        get
        {
            return this._ProgressThickness;
        }
        set
        {
            this._ProgressThickness = value;
            this.Invalidate();
        }
    }

    public Color ProgressNotCompletedColor
    {
        get
        {
            return this._ProgressNotCompleted;
        }
        set
        {
            this._ProgressNotCompleted = value;
            this.Invalidate();
        }
    }

    public Color ProgressCompletedColor
    {
        get
        {
            return this._ProgressCompletedColor;
        }
        set
        {
            this._ProgressCompletedColor = value;
            this.Invalidate();
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        // Call the OnPaint method of the base class.
        base.OnPaint(e);

        DrawProgress(e.Graphics, new Rectangle(new Point(1,1), new Size(this.ClientSize.Width - 3, this.ClientSize.Height - 3)), PercentageCompleted);
    }

    private void DrawProgress(Graphics g, Rectangle rec, Single percentage)
    {  
        Single progressAngle = (360 / 100 * percentage);
        Single remainderAngle = 360 - progressAngle;

        try
        {
            using (Pen progressPen = new Pen(ProgressCompletedColor, ProgressBarThickness), remainderPen = new Pen(ProgressNotCompletedColor, ProgressBarThickness))
            {
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.DrawArc(progressPen, rec, -90, progressAngle);
                g.DrawArc(remainderPen, rec, progressAngle - 90, remainderAngle);
            }
        }
        catch (Exception exc) { }
    }

}

您正在用整数计算角度。当你这样做时:

angle = 360 / 100 * percentage;

意思是

angle = 3 * percentage;

这当然会导致错误。如果您想继续使用整数,有一个简单的修复方法:

angle = 360 * percentage / 100;

这样它就不会在乘法前四舍五入。或者你可以一直使用浮点数:

angle = 360f / 100f * percentage;