实例介绍
【实例简介】C# GDI 绘制圆形进度条制作自定义控件
【实例截图】
【核心代码】
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace ProgressCircleControl
{
/// <summary>
/// 圆形进度条
/// </summary>
public class ProgressCircle : Control
{
#region variables
Timer timer;
Graphics canvasGraphic;
Image canvas;
Image icon;
Rectangle rect;
int blinkCount = 2;
int blinkSpeed = 10;
Pen spokePen = Pens.White;
int spokeCount = 4;
Brush pieBrush = Brushes.LawnGreen;
int value = 0;
PointF center;
#endregion
#region properties
[DefaultValue(2), Category("Appearance"), Description("Blink count. 0 for disable.")]
public int BlinkCount
{
get { return blinkCount; }
set
{
if (value > -1)
{
blinkCount = value; Refresh();
}
}
}
[DefaultValue(10), Category("Appearance"), Description("Blink speed.")]
public int BlinkSpeed
{
get { return blinkSpeed; }
set
{
if (value > 0 && value < 256)
blinkSpeed = value;
}
}
[DefaultValue(4), Category("Appearance"), Description("Spoke count. Maximum value is 10. 0 for disable.")]
public int SpokeCount
{
get { return spokeCount; }
set
{
spokeCount = value;
Invalidate();
}
}
[DefaultValue(typeof(Color), "White"), Category("Appearance"), Description("Color of spokes.")]
public Color SpokeColor
{
get { return spokePen.Color; }
set
{
spokePen.Color = value;
Invalidate();
}
}
[Category("Appearance"), Description("Icon to display. Maximum size is half of the control.")]
public Image Image
{
get { return icon; }
set
{
icon = value;
Invalidate();
}
}
[DefaultValue(0), Category("Behavior"), Description("Value of percentage.")]
public int Value
{
get { return value; }
set
{
if (value < 0) value = 0;
if (value > 100) value = 100;
this.value = value;
Invalidate();
if (value == 100 && BlinkCount != 0)
timer.Start();
else
if (timer.Enabled)
timer.Stop();
}
}
private Color valueTextColor = Color.OrangeRed;
[Category("Behavior"), Description("进度值的文字颜色,默认是橙红色.")]
public Color ValueTextColor
{
get { return valueTextColor; }
set { valueTextColor = value; }
}
#endregion
#region initiators
public ProgressCircle()
{
DoubleBuffered = true;
Width = 64;
ForeColor = Color.FromArgb(146, 223, 99);
spokePen = new Pen(BackColor, 1);
pieBrush = new SolidBrush(ForeColor);
BackColor = Color.White;
ForeColor = Color.LawnGreen;
BlinkCount = 2;
SpokeColor = Color.White;
SpokeCount = 4;
center = new PointF();
timer = new Timer();
timer.Interval = 10;
timer.Tick = new EventHandler(timer_Tick);
OnResize(null);
}
#endregion
#region callbacks
int blink = 0;
int count = 255;
void timer_Tick(object sender, EventArgs e)
{
if (!timer.Enabled)
return;
if (blink == BlinkCount)
{
blink = 0;
count = 255;
timer.Stop();
Refresh();
}
else
{
count -= 5;
if (count == 0)
{
count = 255;
blink ;
}
else
{
Refresh();
}
}
}
#endregion
#region painters
void drawIcon(Graphics g, Image img)
{
if (img == null)
return;
g.DrawImageUnscaled(img, (this.ClientSize.Width - img.Width) / 2, (this.ClientSize.Height - img.Height) / 2);
}
void drawSpokes(Graphics g, int count)
{
if (count < 1)
return;
if (count > 10)
count = 10;
float angleStep = 360f / (count * 2);
float angle = 0f;
for (int i = 0; i < count; i )
{
PointF p1 = angleToPoint(angle, rect.Width / 2);
PointF p2 = angleToPoint(angle 180f, rect.Width / 2);
g.DrawLine(spokePen, p1, p2);
angle = angleStep;
}
}
void drawProgress(Graphics g, float percent)
{
if (percent < 0 || percent > 100)
return;
g.FillPie(
pieBrush,
rect,
-90,
(float)(360f * Value / 100f)
);
}
void drawBackground(Graphics g)
{
g.Clear(Color.Transparent);
((SolidBrush)pieBrush).Color = BackColor;
g.FillEllipse(pieBrush, rect);
((SolidBrush)pieBrush).Color = this.ForeColor;
}
#endregion
#region helpers
PointF angleToPoint(float angle, float radius)
{
return new PointF(
center.X radius * (float)Math.Cos(angle * Math.PI / 180),
center.Y radius * (float)Math.Sin(angle * Math.PI / 180)
);
}
#endregion
#region events
protected override void OnBackColorChanged(EventArgs e)
{
base.OnBackColorChanged(e);
}
protected override void OnForeColorChanged(EventArgs e)
{
base.OnForeColorChanged(e);
((SolidBrush)pieBrush).Color = ForeColor;
}
protected override void OnResize(EventArgs e)
{
this.Height = this.Width;
rect = ClientRectangle;
rect.Width -= 1;
rect.Height -= 1;
center = new PointF(rect.Width / 2, rect.Height / 2);
if (!this.ClientSize.IsEmpty)
{
if (canvas != null)
canvas.Dispose();
canvas = new Bitmap(rect.Width, rect.Height);
if (canvasGraphic != null)
canvasGraphic.Dispose();
canvasGraphic = Graphics.FromImage(canvas);
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.Clear(Parent.BackColor);
canvasGraphic.SmoothingMode = SmoothingMode.HighQuality;
drawBackground(canvasGraphic);
if (!timer.Enabled || blink == BlinkCount)
drawProgress(canvasGraphic, value);
else
{
((SolidBrush)pieBrush).Color = Color.FromArgb(count, ForeColor.R, ForeColor.G, ForeColor.B);
canvasGraphic.FillEllipse(pieBrush, rect);
((SolidBrush)pieBrush).Color = ForeColor;
}
drawSpokes(canvasGraphic, SpokeCount);
if (icon != null)
drawIcon(canvasGraphic, icon);
e.Graphics.DrawImageUnscaled(canvas, (ClientSize.Width - canvas.Width) / 2, (ClientSize.Height - canvas.Height) / 2);
if (value != 0)
{
var rect = ClientRectangle;
rect.Inflate(0 - Width / 2, 0 - Height / 2);
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawString(value.ToString() "%", new Font("微软雅黑", 18F, FontStyle.Bold), new SolidBrush(ValueTextColor), rect, sf);
}
}
#endregion
}
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论