首先添加一个timer,50s
namespace High_Tech_Watch
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
int[] oldLine;
int SIZE = 15; //方格的大小
Pen LINEPEN = new Pen(Color.FromArgb(3,64, 129), 1); //背景线条颜色
Pen FORELINEPEN = new Pen(Color.LightBlue); //前景线条颜色
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int Bvalue;
Bvalue = Value;
if (shake != 0)
{
Random ro = new Random();
int r = ro.Next(0, shake);
Value += (ro.Next(-shake, 0) / 2) + r/2;
if (Value>100)
{
Value = 100;
}
if (Value < 0)
{
Value = 0;
}
}
int h = (int)(this.Size.Height / SIZE);
int w = (int)(this.Size.Width / SIZE )+ 1;//这里加1保证了滚动时最右侧垂直线及时出现
for (; h >= 0;h-- )
{
g.DrawLine(LINEPEN, new Point(0, h * SIZE), new Point(this.Size.Width, h * SIZE));
}
for (; w>=0;w-- )
{
g.DrawLine(LINEPEN, new Point((w * SIZE) - limits, 0), new Point((w * SIZE) - limits, this.Size.Height));
}
for (int i = oldLine.Length - 1,j = 0;i >j ;j++ )
{
g.DrawLine(FORELINEPEN, new Point(j,(this.Height - (int)(((float)oldLine[j] / (float)100) * (float)this.Height) ) -1),
new Point(j + 1, (this.Height - (int)(((float)oldLine[j+1] / (float)100) * (float)this.Height))-1) );
}
for (int i = oldLine.Length - 1, j = 0; i > j; j++)
{
oldLine[j] = oldLine[j + 1];
}
oldLine[oldLine.Length - 1] = Value;
pintLightPoint(e);
Value = Bvalue;
}
private void pintLightPoint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(global::High_Tech_Watch.Resource1.未标题_2,new Rectangle(new Point(this.Width - 50,this.Height - (int)(((float)lightPointValue / (float)100) * (float)this.Height ) - 10),new Size(20,20)));
}
int lightPointValue = 50;
int limits = 0;//滚动就靠他了,是一个范围
private void timer1_Tick(object sender, EventArgs e)
{
limits++;
if (limits >= SIZE)
{
limits = 0;
}
this.Invalidate();
}
private void UserControl1_Load(object sender, EventArgs e)
{
oldLine = new int[this.Width - 40];
}
int shake = 0;
[DefaultValue(0),Description("抖动率,值控件输入的值自动抖动(禁用是为0)"),Category("属性值")]
public int Shake
{
get{return shake;}
set{shake = value;}