如何在C#winform中使用CircularProgressBar
我需要一个函数来显示对话框中的进度环,当查询数据库中的数据时,传统的进度条对我来说是不好的,因为我不知道等待时间会持续多久。 所以我从Nuget安装了CircularProgressBar来显示进度环。 但是没有示例代码,我不知道如何使用它。 以前有人用过这个控件吗? 我的问题是: 1.如何将此控件拖动到工具箱中,现在我无法在visual studio 2015的工具箱中看到此CircularProgressBar控件,即使我已在项目中看到此引用; 2.如何调用它或编写一些显示/关闭代码来显示它,并在对话框中需要时关闭它。 谢谢,你可以看看。该文章中的示例代码是:如何在C#winform中使用CircularProgressBar,c#,winforms,progress-bar,C#,Winforms,Progress Bar,我需要一个函数来显示对话框中的进度环,当查询数据库中的数据时,传统的进度条对我来说是不好的,因为我不知道等待时间会持续多久。 所以我从Nuget安装了CircularProgressBar来显示进度环。 但是没有示例代码,我不知道如何使用它。 以前有人用过这个控件吗? 我的问题是: 1.如何将此控件拖动到工具箱中,现在我无法在visual studio 2015的工具箱中看到此CircularProgressBar控件,即使我已在项目中看到此引用; 2.如何调用它或编写一些显示/关闭代码来显示它
#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
#区域包括
使用制度;
使用系统集合;
使用System.Collections.Generic;
使用系统组件模型;
使用系统图;
使用System.Drawing.Drawing2D;
使用System.Drawing.Text;
使用系统文本;
使用System.Windows.Forms;
#端区
#区域循环压杆
公共类CircularProgressBar:控件
{
#区域枚举
公共枚举形状
{
圆形的
平的
}
#端区
#区域变量
私人长期价值;
专用长_最大值=100;
private Color _ProgressColor1=Color.FromArgb(92,92,92);
private Color _ProgressColor2=Color.FromArgb(92,92,92);
private_ProgressShape ProgressShapeVal;
#端区
#区域自定义属性
公共长期价值
{
获取{返回_值;}
设置
{
如果(值>最大值)
值=_最大值;
_价值=价值;
使无效();
}
}
公共长线最大值
{
获取{return\u max;}
设置
{
如果(值<1)
数值=1;
_最大值=最大值;
使无效();
}
}
公共颜色1
{
获取{return\u ProgressColor1;}
设置
{
_ProgressColor1=数值;
使无效();
}
}
公共颜色2
{
获取{return\u ProgressColor2;}
设置
{
_ProgressColor2=数值;
使无效();
}
}
公共_progressshapeprogressshape
{
获取{return ProgressShapeVal;}
设置
{
ProgressShapeVal=值;
使无效();
}
}
#端区
#区域事件参数
受保护的覆盖void OnResize(事件参数e)
{
基数(e);
设置标准尺寸();
}
IzeChanged上的受保护覆盖无效(EventArgs e)
{
基地.OnSizeChanged(e);
设置标准尺寸();
}
PaintBackground上受保护的覆盖无效(PaintEventArgs p)
{
基于绘画背景(p);
}
#端区
公共通告
{
尺寸=新尺寸(130130);
字体=新字体(“Segoe UI”,15);
最小尺寸=新尺寸(100100);
双缓冲=真;
}
私有void SetStandardSize()
{
int _Size=Math.Max(宽度、高度);
尺寸=新尺寸(_尺寸,_尺寸);
}
公共无效增量(int Val)
{
这是._值+=Val;
使无效();
}
公共无效递减率(int Val)
{
此值为._值-=Val;
使无效();
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
基础漆(e);
使用(位图位图=新位图(this.Width,this.Height))
{
使用(Graphics=Graphics.FromImage(位图))
{
graphics.SmoothingMode=SmoothingMode.AntiAlias;
图形。清晰(此。背景色);
使用(LinearGradientBrush笔刷=新的LinearGradientBrush(this.ClientRectangle,this.\u ProgressColor1,this.\u ProgressColor2,LinearGradientMode.ForwardDiagonal))
{
使用(画笔=新画笔(画笔,14f))
{
开关(this.ProgressShapeVal)
{
案例_ProgressShape.Round:
pen.StartCap=线头圆形;
pen.EndCap=LineCap.Round;
打破
案例_ProgressShape.Flat:
pen.StartCap=扁平线帽;
pen.EndCap=LineCap.Flat;
打破
}
graphics.DrawArc(画笔,0x12,0x12,(this.Width-0x23)-2,(this.Height-0x23)-2,-90,(int)Math.Round((双精度)((360.0/(双精度)this.u最大值))*this.u值));
}
}
使用(lineArgRadientBrush2=新的LinearGradientBrush(this.ClientRectangle,Color.FromArgb(0x34,0x34,0x34),Color.FromArgb(0x34,0x34),LinearGradientMode.Vertical))
{
graphics.FillEllipse(画笔2,0x18,0x18,(this.Width-0x30)-1,(this.Height-0x30)-1);
}
SizeF MS=graphics.MeasureString(Convert.ToString(Convert.ToInt32((100/_最大值)*_值)),字体);
graphics.DrawString(Convert.ToString(Convert.ToInt32((100/_最大值)*_值))、字体、画笔.白色、Convert.ToInt32(宽度/2-MS.Width/2)、Convert.ToInt32(高度/2-MS.Height/2));
e、 Graphics.DrawImage(位图,0,0);
graphics.Dispose();
Dispose();
}
}
}
}
#端区
然后,您可以在希望显示progressbar的任何位置使用此控件。
更新
如何使用:
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);