我如何在c#中制作圆形图?

时间:2015-05-25 15:50:38

标签: c# animation graph

你好我正在创建一个应用程序,设计需要根据给定值改变的特殊图形。像这样:

enter image description here

enter image description here

enter image description here

我该怎么做呢?有人能指出我正确的方向吗? 橙色的外环随值变化但中间圆保持静止。

p.s我的绘画技巧很棒。

1 个答案:

答案 0 :(得分:3)

您可以轻松地绘制自己的控件。主要的是FillPie方法,它只绘制圆的一部分。要更改外圈或填充方向的起点,您需要在FillPie事件的OnPaint调用中更改起始角度和扫描角度。

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

public class CircularProgressBar : Control
{

    #region "Properties"
    private Color _BorderColor;
    public Color BorderColor
    {
        get { return _BorderColor; }
        set
        {
            _BorderColor = value;
            this.Invalidate();
        }
    }

    private Color _InnerColor;
    public Color InnerColor
    {
        get { return _InnerColor; }
        set
        {
            _InnerColor = value;
            this.Invalidate();
        }
    }

    private bool _ShowPercentage;
    public bool ShowPercentage
    {
        get { return _ShowPercentage; }
        set
        {
            _ShowPercentage = value;
            this.Invalidate();
        }
    }

    private int _BorderWidth;
    public int BorderWidth
    {
        get { return _BorderWidth; }
        set
        {
            _BorderWidth = value;
            this.Invalidate();
        }
    }

    private float _Value;
    public float Value
    {
        get { return _Value; }
        set
        {
            _Value = value;
            this.Invalidate();
        }
    }

    #endregion

    #region "Constructor"
    public CircularProgressBar()
    {
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.ResizeRedraw, true);
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        _Value = 100;
        _BorderColor = Color.Orange;
        _BorderWidth = 30;
        _ShowPercentage = true;
        _InnerColor = Color.DarkGray;
        this.ForeColor = Color.White;
    }
    #endregion

    #region "Painting"
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

        //Measure the single parts
        int Diameter = Math.Min(this.ClientSize.Width, this.ClientSize.Height);
        int InnerDiameter = Diameter - BorderWidth;
        Rectangle PieRect = new Rectangle(Convert.ToInt32(this.ClientSize.Width / 2 - Diameter / 2), Convert.ToInt32(this.ClientSize.Height / 2 - Diameter / 2), Diameter, Diameter);
        Rectangle InnerRect = new Rectangle(Convert.ToInt32(this.ClientSize.Width / 2 - InnerDiameter / 2), Convert.ToInt32(this.ClientSize.Height / 2 - InnerDiameter / 2), InnerDiameter, InnerDiameter);

        //Draw outer ring
        using (SolidBrush b = new SolidBrush(BorderColor))
        {
            e.Graphics.FillPie(b, PieRect, 0, Value / 100 * 360);
        }

        //Draw inner ring
        using (SolidBrush b = new SolidBrush(this._InnerColor))
        {
            e.Graphics.FillEllipse(b, InnerRect);
        }

        //Draw percentage
        if (ShowPercentage)
        {
            using (StringFormat sf = new StringFormat())
            {
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                using (SolidBrush b = new SolidBrush(this.ForeColor))
                {
                    e.Graphics.DrawString(Convert.ToInt32(Value).ToString() + "%", this.Font, b, InnerRect, sf);
                }
            }
        }
    }
    #endregion

}

结果: enter image description here