HuronControl.LoadingCircle()
功能:支持渐变、步进方式的“载入中”控件。
支持自定义:旋转速度(Marquee方式有效),线条颜色,线条数量,内外半径,线条宽度。
该控件继承自System.Windows.Forms.Control
效果演示:
图1 控件演示
图2 仿Apple半透明Loading控件
注:图2 所示效果和 LoadingCircle 调用方法将在 HuronControl.RoundcornerForm() 中作介绍。http://blog.csdn.net/huronzhu/article/details/6981647
变量
private Timer _Timer;//循环timer
private bool _IsLoading = false;//指示是否正在运行
private int _NumberOfSpoke = 12;//辐条数目
private int _lineWidth = 3;//线条宽度
private int _InnerCircleRadius = 5;//内半径
private int _OutnerCircleRadius = 10;//外半径
private int _ProgressValue = 0;//循环进度值
private PointF _CenterPointF;//中心坐标
private Color _ThemeColor = Color.DimGray;//线条颜色
private int _Speed = 100;//循环速度(毫秒)
private Color[] _Colors;//渐变颜色集
private bool _IsStep = false;//是否为步进方式
private int _Step = 0;//当前步进到
private double _Percent = 0;//总数量的单位值
构造函数和重写方法
public LoadingCircle()
{
//InitializeComponent();
SetStyle(ControlStyles.UserPaint
| ControlStyles.OptimizedDoubleBuffer
| ControlStyles.ResizeRedraw
| ControlStyles.SupportsTransparentBackColor, true);
InitEnv();
}
//初始化环境
private void InitEnv()
{
_ProgressValue = 0;
_Timer = new Timer();
_Timer.Tick += new EventHandler(_Timer_Tick);
_Timer.Interval = _Speed;
_Colors = GetColors(_ThemeColor, _NumberOfSpoke, _IsLoading);
Invalidate();
}
protected override void OnPaint(PaintEventArgs pe)
{
_CenterPointF = new PointF(this.Width / 2, this.Height / 2);
if (_NumberOfSpoke > 0)
{
pe.Graphics.SmoothingMode = SmoothingMode.HighQuality;
double offsetAngle = 360 / (double)_NumberOfSpoke;
double currentAngle = _ProgressValue * offsetAngle - 90;
if (_IsStep)
{
for (int i = 0; i < _Step; i++)
{
DrawLine(pe.Graphics, GetPointF(_CenterPointF, _InnerCircleRadius, currentAngle),
GetPointF(_CenterPointF, _OutnerCircleRadius, currentAngle), _ThemeColor);
currentAngle += offsetAngle;
}
}
else
{
for (int i = 0; i < _NumberOfSpoke; i++)
{
DrawLine(pe.Graphics, GetPointF(_CenterPointF, _InnerCircleRadius, currentAngle),
GetPointF(_CenterPointF, _OutnerCircleRadius, currentAngle), _Colors[i]);
currentAngle += offsetAngle;
}
}
}
base.OnPaint(pe);
}
辅助方法
//画带有圆角线帽和指定颜色的直线(带alpha)
private void DrawLine(Graphics g, PointF pointF1, PointF pointF2, Color color)
{
//强制资源管理 (管理非托管资源)
using (Pen pen = new Pen(new SolidBrush(color), _lineWidth))
{
//指定线帽子
pen.StartCap = LineCap.Round;
pen.EndCap = LineCap.Round;
g.DrawLine(pen, pointF1, pointF2);
}
}
//根据中心点 半径 和角度,获取从中心点出发的线段终点
private PointF GetPointF(PointF centerPointF, int r, double angle)
{
double A = Math.PI * angle / 180;//(angle/360)*2PI
float xF = centerPointF.X + r * (float)Math.Cos(A);
float yF = centerPointF.Y + r * (float)Math.Sin(A);
return (new PointF(xF, yF));
}
//根据辐条数量和主题颜色返回一个Color数组
private Color[] GetColors(Color color, int spokeMember, bool isLoading)
{
Color[] colors = new Color[spokeMember];
int offseAlpha = 255 / spokeMember;//alpha偏差量
if (isLoading == true)
{
for (int i = 0; i < spokeMember; i++)
{
colors[i] = Color.FromArgb(i * offseAlpha, color);//反向储存 形成顺时针旋转效果
}
}
else
{
for (int i = 0; i < spokeMember; i++)
{
colors[i] = color;
}
}
return colors;
}
private void _Timer_Tick(object sender, EventArgs e)
{
_ProgressValue++;
if (_ProgressValue == _NumberOfSpoke)
_ProgressValue = 0;
Invalidate();
}
控制方法
/// <summary>
/// 开始循环
/// </summary>
public void StartMarquee()
{
_IsLoading = true;
InitEnv();
_Timer.Start();
}
/// <summary>
/// 开始步进
/// </summary>
/// <param name="Value">总步数</param>
public void StartStep(int Value)
{
_Percent = (double)(_NumberOfSpoke) / Value;
_Step = 0;
_IsLoading = true;
_IsStep = true;
InitEnv();
//_Timer.Start();
}
/// <summary>
/// 步进到某处
/// </summary>
/// <param name="Value">当前值</param>
public void PerformStep(int Value)
{
_Step = (int)(Value * _Percent);
Invalidate();
}
/// <summary>
/// 控件循环停止
/// </summary>
public void Stop()
{
_Timer.Stop();
_IsLoading = false;
_IsStep = false;
InitEnv();
}
公共属性
/// <summary>
/// 获取和设置循环速度(单位毫秒)
/// </summary>
public int Speed
{
get { return _Speed; }
set
{
_Speed = value;
_Timer.Interval = _Speed;
}
}
/// <summary>
/// 获取和设置线条数量
/// </summary>
public int SpokesMember
{
get { return _NumberOfSpoke; }
set
{
_NumberOfSpoke = value;
InitEnv();
}
}
/// <summary>
/// 获取和设置圆形内半径
/// </summary>
public int InnerCircleRadius
{
get { return _InnerCircleRadius; }
set
{
_InnerCircleRadius = value;
Invalidate();
}
}
/// <summary>
/// 获取和设置圆形内半径
/// </summary>
public int OutnerCircleRadius
{
get { return _OutnerCircleRadius; }
set
{
_OutnerCircleRadius = value;
Invalidate();
}
}
/// <summary>
/// 获取和设置主题颜色
/// </summary>
public Color ThemeColor
{
get { return _ThemeColor; }
set
{
_ThemeColor = value;
_Colors = GetColors(_ThemeColor, _NumberOfSpoke, _IsLoading);
Invalidate();
}
}
/// <summary>
/// 获取和设置线条宽度
/// </summary>
public int LineWidth
{
get { return _lineWidth; }
set
{
_lineWidth = value;
Invalidate();
}
}
/// <summary>
/// 获取控件运行状态,true:正在运行,false:已停止
/// </summary>
public bool IsActive
{
get
{
return _Timer.Enabled;
}
}