/*******************************************************************************
* ProgressCircle.cs
* A Google Chrome style progress indicator.
* -----------------------------------------------------------------------------
* Project: Conmajia.Controls
* Author: Conmajia
* Url: conmajia@gmail.com
* Initiate: 25th July, 2012
* Version: 1.0.0.0
* History:
* 25th July, 2012
* Initiated.
* ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace ProgressCircleControl
{
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;
Refresh();
}
}
[DefaultValue(typeof(Color), "White"), Category("Appearance"), Description("Color of spokes.")]
public Color SpokeColor
{
get { return spokePen.Color; }
set
{
spokePen.Color = value;
Refresh();
}
}
[Category("Appearance"), Description("Icon to display. Maximum size is half of the control.")]
public Image Image
{
get { return icon; }
set
{
//if (value != null && (value.Width > this.Width / 2 && value.Height > this.Height / 2))
// return;
icon = value;
Refresh();
}
}
[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;
Refresh();
if (value == 100 && BlinkCount != 0)
timer.Start();
else
if (timer.Enabled)
timer.Stop();
}
}
#endregion
#region initiators
public ProgressCircle()
{
this.DoubleBuffered = true;
this.Width = 32;
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);
// draw shadow
// not ready
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 = this.ClientRectangle;
rect.Width -= 1;
rect.Height -= 1;
//rect.X = (this.ClientSize.Width - rect.Width) / 2;
//rect.Y = (this.ClientSize.Height - rect.Height) / 2;
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(this.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,
(this.ClientSize.Width - canvas.Width) / 2,
(this.ClientSize.Height - canvas.Height) / 2
);
}
#endregion
}
}
仿 Chrome 进度条
最新推荐文章于 2024-09-18 14:02:14 发布