一个C#版的类似迅雷下载显示控件
近几日,闲来无时感觉迅雷的下载进度显示模块做的不错,心血来潮,就简单写了一个控件。
我先直接上源码:
程序有注释的,第一次写,水平不高请各们高手指教。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace Liyou.Progess
{
public partial class RectangleProgess : UserControl
{
///
/// 网格列数
///
private int cols = 50;
///
/// 网格行数
///
private int rows = 100;
///
/// 存储变量的数组,其中X表示第几列,Y表示值
///
private Point[] _Point;
///
/// 控件的背景色
///
private Color drawcolor = Color.Green;
///
/// 控件当前值
///
private int _value=0;
///
/// 值
///
public int DisplayValue
{
get
{
return _value;
}
set
{
_value = value;
}
}
///
/// 绘制色
///
public Color DrawColor
{
get
{
return drawcolor;
}
set
{
drawcolor = value;
}
}
///
/// 是否开始显示进度
///
public bool EnableDisplay
{
get
{
return timer1.Enabled;
}
set
{
timer1.Enabled = value;
}
}
///
/// 返回和设计行值
///
public int Rows
{
get
{
return rows;
}
set
{
if(value>=0&&value<=100)
rows = value;
}
}
///
/// 高
///
public int H
{
get
{
return this.Height;
}
set
{
this.Height = value;
}
}
///
/// 宽
///
public int W
{
get
{
return this.Width;
}
set
{
this.Width = value;
}
}
///
///初始化
///
public RectangleProgess()
{
InitializeComponent();
//初始化变量
_Point = new Point[cols];
InitPoint();
}
private void InitPoint()
{
//初始化变量,每个赋0值
for (int i = 0; i < _Point.Length; i++)
{
_Point[i] = new Point(i, 0);
}
}
///
/// 关键代码,移位
///根据新传入的变量值依次向左移一位,最左一位的数据丢弃
///
///
private void MoveBit(int value)
{
for (int i = _Point.Length - 1; i > 0; i--)
{
_Point[i] = new Point(i,_Point[i - 1].Y);
}
_Point[0] = new Point(0, value);
}
///
/// 画图,进行显示
///
private void Display()
{
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
g.Dispose();
for (int i = 0; i < _Point.Length; i++)
{
DrawFromPoint(_Point[i]);
}
}
///
/// 根据变量数组进行画图
///
///
private void DrawFromPoint(Point pi)
{
Graphics g = this.CreateGraphics();
SolidBrush brush = new SolidBrush(DrawColor);
Point pp=GetDrawPoint(pi);
g.FillRectangle(brush, pp.X, pp.Y, W / cols, pi.Y * H / rows);
g.Dispose();
}
///
/// 计算相应点在画图中的坐标
///
///
///
private Point GetDrawPoint(Point pi)
{
int x = W - (pi.X + 1) * W / cols;
int y = H - (pi.Y * H / Rows);
return new Point(x, y);
}
private void timer1_Tick(object sender, EventArgs e)
{
MoveBit(DisplayValue);
Display();
}
}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
