我写了一个动态加载label的方法,同时我想让他们都闪烁,该怎么办啊? 求助大大门 !!!
Label lbl;
public void painLabel()
{
for (int r = 1; r <= 5; r++)
{
for (int c = 1; c <= 6; c++)
{
lbl = new Label();
lbl.Name = "lbl" + r + "-" + c;
lbl.Text = r + "-" + c;
lbl.AutoSize = true;
lbl.Font = new Font("宋体", 13.75F,System.Drawing.FontStyle.Bold);
lbl.ForeColor = System.Drawing.Color.Blue;
lbl.Location = new Point((90 * c), (50 * r));
this.Controls.Add(lbl);
}
}
}
Label lbl;
public void painLabel()
{
for (int r = 1; r <= 5; r++)
{
for (int c = 1; c <= 6; c++)
{
lbl = new Label();
lbl.Name = "lbl" + r + "-" + c;
lbl.Text = r + "-" + c;
lbl.AutoSize = true;
lbl.Font = new Font("宋体", 13.75F,System.Drawing.FontStyle.Bold);
lbl.ForeColor = System.Drawing.Color.Blue;
lbl.Location = new Point((90 * c), (50 * r));
this.Controls.Add(lbl);
}
}
}
那要是一个个循环的设置visible属性实现闪动的话,效率肯定很低。建议搂主把这些Label放到一个Panel里面,然后控制Panel控件的Visible属性就可以了
效率还很高。下面是对创建过程的修改:
Label lbl;
Panel pnlLabelContainer = null;
public void painLabel()
{
if(this.pnlLabelContainer == null)
{
this.pnlLabelContainer = new Panel();
//然后搂主在这里设置这个Panel的位置和大小
this.pnlLabelContainer.SetBounds(.......);
this.Controls.Add(this.pnlLabelContainer);
}
for (int r = 1; r <= 5; r++)
{
for (int c = 1; c <= 6; c++)
{
lbl = new Label();
lbl.Name = "lbl" + r + "-" + c;
lbl.Text = r + "-" + c;
lbl.AutoSize = true;
lbl.Font = new Font("宋体", 13.75F,System.Drawing.FontStyle.Bold);
lbl.ForeColor = System.Drawing.Color.Blue;
lbl.Location = new Point((90 * c), (50 * r));
this.pnlLabelContainer.Controls.Add(lbl);
}
}
} //下面是对timer事件的更改
private void timer1_Tick(object sender, EventArgs e)
{
if(this.pnlLabelContainer == null)
return;
this.pnlLabelContainer.Visible = !this.pnlLabelContainer.Visible;
}这样就能较好的实现你的闪动啦,搂主可以试试
private void Form1_Load(object sender, System.EventArgs e)
{
//点颜色数组的创建
brushs=new Brush[4];//实例颜色数组
//添加颜色到数组
brushs[0]=Brushes.Red;
brushs[1]=Brushes.Yellow;
brushs[2]=Brushes.Blue;
brushs[3]=Brushes.Green;
//颜色的数组创建
colors = new Color[4]{Color.Red,Color.Blue,Color.Yellow,Color.Green};
td=new Thread(new ThreadStart(ShowP));//创建线程对象确定随机生成点的方法
td1=new Thread(new ThreadStart(ShowL));//创建线程对象确定动态显示标签的方法
td.Start();
td1.Start();
}
//随机生成点的方法
private void ShowP()
{
Random rnd=new Random();
int r=0;//保存数组下标
int positionx=0;//保存点的X坐标
int positiony=0;//保存点的Y坐标
while(true)
{
Graphics g=this.CreateGraphics();
r=rnd.Next(0,4);
positionx=rnd.Next(0,this.Width);
positiony=rnd.Next(0,this.Height);
g.FillEllipse(brushs[r],positionx,positiony,8,8);//绘制点
Thread.Sleep(100);
}
} //动态显示标签的方法
private void ShowL()
{
Random rnd=new Random();
int x=this.label1.Left;//获取标签的原位x坐标
int y=this.label1.Top;//获取标签的原位y坐标
int colorIndex;
while(true)
{
for(int i=0;i<3;i++)
{
colorIndex=rnd.Next(0,4);
this.label1.Left+=1;
this.label1.Top+=1;
this.label1.ForeColor=colors[colorIndex];
Thread.Sleep(100);
}
this.label1.Left=x;
this.label1.Top=y;
} } private void Form1_Closed(object sender, System.EventArgs e)
{
td.Abort();
td1.Abort();
}
这是一个闪动的例子,可以参考一下!