如何在C#winform中使用CircularProgressBar

时间:2017-11-14 06:50:52

标签: c# winforms progress-bar

我需要一个函数来在数据库中查询数据时在对话框中显示进度环,传统的进度条对我不利,因为我不知道等待时间将持续多长时间。 所以我从Nuget安装了CircularProgressBar来显示进度环。 但是没有示例代码,我不知道如何使用它。 有没有人之前使用过此控件? 我的问题是: 1.如何将此控件拖动到工具箱中,现在我无法在visual studio 2015的工具箱中看到此CircularProgressBar控件,即使我已在项目中看到此引用; 2.如何调用它或编写一些show / close代码来显示它并在我需要它时在对话框中关闭它。 感谢,

2 个答案:

答案 0 :(得分:1)

你可以看看this article。该文章中的示例代码是:

#region Includes
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Text;
using System.Windows.Forms;
#endregion

#region CircularProgressBar

public class CircularProgressBar : Control
{

    #region Enums

    public enum _ProgressShape
    {
        Round,
        Flat
    }

    #endregion
    #region Variables

    private long _Value;
    private long _Maximum = 100;
    private Color _ProgressColor1 = Color.FromArgb(92, 92, 92);
    private Color _ProgressColor2 = Color.FromArgb(92, 92, 92);
    private _ProgressShape ProgressShapeVal;

    #endregion
    #region Custom Properties

    public long Value
    {
        get { return _Value; }
        set
        {
            if (value > _Maximum)
                value = _Maximum;
            _Value = value;
            Invalidate();
        }
    }

    public long Maximum
    {
        get { return _Maximum; }
        set
        {
            if (value < 1)
                value = 1;
            _Maximum = value;
            Invalidate();
        }
    }

    public Color ProgressColor1
    {
        get { return _ProgressColor1; }
        set
        {
            _ProgressColor1 = value;
            Invalidate();
        }
    }

    public Color ProgressColor2
    {
        get { return _ProgressColor2; }
        set
        {
            _ProgressColor2 = value;
            Invalidate();
        }
    }

    public _ProgressShape ProgressShape
    {
        get { return ProgressShapeVal; }
        set
        {
            ProgressShapeVal = value;
            Invalidate();
        }
    }

    #endregion
    #region EventArgs

    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        SetStandardSize();
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        SetStandardSize();
    }

    protected override void OnPaintBackground(PaintEventArgs p)
    {
        base.OnPaintBackground(p);
    }

    #endregion

    public CircularProgressBar()
    {
        Size = new Size(130, 130);
        Font = new Font("Segoe UI", 15);
        MinimumSize = new Size(100, 100);
        DoubleBuffered = true;
    }

    private void SetStandardSize()
    {
        int _Size = Math.Max(Width, Height);
        Size = new Size(_Size, _Size);
    }

    public void Increment(int Val)
    {
        this._Value += Val;
        Invalidate();
    }

    public void Decrement(int Val)
    {
        this._Value -= Val;
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        using (Bitmap bitmap = new Bitmap(this.Width, this.Height))
        {
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                graphics.Clear(this.BackColor);
                using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, this._ProgressColor1, this._ProgressColor2, LinearGradientMode.ForwardDiagonal))
                {
                    using (Pen pen = new Pen(brush, 14f))
                    {
                        switch (this.ProgressShapeVal)
                        {
                            case _ProgressShape.Round:
                                pen.StartCap = LineCap.Round;
                                pen.EndCap = LineCap.Round;
                                break;

                            case _ProgressShape.Flat:
                                pen.StartCap = LineCap.Flat;
                                pen.EndCap = LineCap.Flat;
                                break;
                        }
                        graphics.DrawArc(pen, 0x12, 0x12, (this.Width - 0x23) - 2, (this.Height - 0x23) - 2, -90, (int)Math.Round((double)((360.0 / ((double)this._Maximum)) * this._Value)));
                    }
                }
                using (LinearGradientBrush brush2 = new LinearGradientBrush(this.ClientRectangle, Color.FromArgb(0x34, 0x34, 0x34), Color.FromArgb(0x34, 0x34, 0x34), LinearGradientMode.Vertical))
                {
                    graphics.FillEllipse(brush2, 0x18, 0x18, (this.Width - 0x30) - 1, (this.Height - 0x30) - 1);
                }
                SizeF MS = graphics.MeasureString(Convert.ToString(Convert.ToInt32((100 / _Maximum) * _Value)), Font);
                graphics.DrawString(Convert.ToString(Convert.ToInt32((100 / _Maximum) * _Value)), Font, Brushes.White, Convert.ToInt32(Width / 2 - MS.Width / 2), Convert.ToInt32(Height / 2 - MS.Height / 2));
                e.Graphics.DrawImage(bitmap, 0, 0);
                graphics.Dispose();
                bitmap.Dispose();
            }
        }
    }
}

#endregion

然后您可以在任何要显示进度条的位置使用此控件。 的更新

如何使用:

  1. 打开要使用循环进度条的应用程序。

  2. 选择&amp;在解决方案资源管理器中删除项目中的CircularProgressBar.cs文件。

  3. enter image description here

    1. 只需编译您的应用程序&amp;您将在工具箱中看到circularprogressbar选项。
    2. enter image description here 4.使用&amp;像给出默认进度条一样给它赋值。它会起作用。

答案 1 :(得分:1)

如果将最大值设置为3,则打印的百分比值为99% 因此,请修改并稍加检查以使上一个值集达到100%

                int percentvalue = Convert.ToInt32((100 / _Maximum) * _Value);
                // here, check if value == maximum
                if (_Value == _Maximum) percentvalue = 100;

                //Desenhe o texto de progresso:
                Brush FontColor = new SolidBrush(this.ForeColor);
                SizeF MS = graphics.MeasureString(Convert.ToString(percentvalue) + "%", Font);
                graphics.DrawString(Convert.ToString(percentvalue) +"%", Font, FontColor, Convert.ToInt32(Width / 2 - MS.Width / 2), Convert.ToInt32(Height / 2 - MS.Height / 2));
                e.Graphics.DrawImage(bitmap, 0, 0);