日期:2014-05-18 浏览次数:20923 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace MyLittleGame { public partial class Form1 : Form { //定义全局变量 Label label; Thread myThread; //构造方法 public Form1() { InitializeComponent(); } //label下落方法 public void _falling() { CheckForIllegalCrossThreadCalls = false; while (this.label.Top <= this.panel1.Height) { this.label.Top += 1; Thread.Sleep(5); } this.label.Dispose(); myThread.Abort(); } //每间隔一定时间发生 private void timer1_Tick(object sender, EventArgs e) { label = new Label(); label.Height = 10; label.Width = 50; label.Left = new Random().Next(9) * 50; switch(new Random().Next(3)+new Random().Next(2)+1){ case 1:label.BackColor = Color.RoyalBlue;break; case 2:label.BackColor = Color.PowderBlue;break; case 3:label.BackColor = Color.BlueViolet;break; default: label.BackColor = Color.MediumVioletRed; break; } panel1.Controls.Add(label); myThread = new Thread(new ThreadStart(_falling)); myThread.Start(); } //start按钮事件 private void startToolStripMenuItem_Click(object sender, EventArgs e) { timer1.Start(); } } }