using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace KeyGame
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
Random random;//随机数产生器
public static string key;//在类之间传递值
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
random =new Random(20);//实例化Random类对象
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.ControlText;
this.ClientSize = new System.Drawing.Size(492, 501);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "键盘打字训练程序";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
//在form窗体内拖拽一个timer控件,并且把Intreval属性设置为1000;
//启动定时器
this.timer1.Start();
}
private void timer1_Tick(object sender, System.EventArgs e)
{//当计时溢出后
//实例化Label对象l
Label l = new Label();
//设置l的背景色
l.BackColor = Color.Black;
//设置字体
l.Font = new Font("Anial",18);
//字体颜色
l.ForeColor =Color.Red;
l.Width = 25;
l.Height =30;
//添加到Form窗体后并显示
this.Controls.Add(l);
l.Show();
//在窗体内的位置是随机的
l.Left = random.Next(0,500);
//内容也是随机的
l.Text = ((char)(random.Next(65,90))).ToString();
MyLabel ml = new MyLabel(this,l);
//创建并启动线程
Thread t =new Thread(new ThreadStart(ml.MoveLabel));
t.Start();
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
//获得键盘键值的字符串表示形式
key = e.KeyCode.ToString();
//MessageBox.Show(key);
}
}
/// <summary>
/// 自己的编写的移动Form窗体内Label的类
/// </summary>
public class MyLabel
{
Label label;
Form form;
public MyLabel(Form form,Label label)
{
this.form = form;
this.label = label;
}
public void MoveLabel()
{
do
{//每次获得时间片后
//键值相等则删除窗体内的Label
if((this.label.Text.ToUpper()) ==Form1.key)
{
this.form.Controls.Remove(this.label);
//this.label.Dispose();
this.label.Text =null;
this.form.BackColor = Color.Black;
break;
}
//否则
//下移Label
this.label.Top+=5;
//暂停0.1秒
Thread.Sleep(100);
//在规定的时间内没有按键则自动清除Label
if( this.label.Top > this.form.Bottom )
break;
}
while(true);
try
{
//结束当前线程;
Thread.CurrentThread.Abort();
}
catch
{
}
}
}
}