这是我写的一个打字小游戏,程序能运行,但KeyPress无效,请各位大虾帮忙修改一下!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace TypeGame
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
protected override bool ProcessDialogKey(Keys keyData)
{
switch (keyData)
{
case Keys.Left:
break;
}
return base.ProcessDialogKey(keyData);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// button1
//
this.button1.Location = new System.Drawing.Point(624, 280);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(720, 301);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void timer1_Tick(object sender, System.EventArgs e)
{
Label lab=new Label();
Random ran=new Random(20);
int result=ran.Next(0,this.Width);
int ch=ran.Next(65,91);
lab.AutoSize=true;
lab.Location=new System.Drawing.Point(result,0);
lab.Name=Convert.ToChar(result).ToString();
lab.Text = Convert.ToChar(ch).ToString();
lab.Font = new Font("宋体",16);
lab.ForeColor = Color.White;
this.Controls.Add(lab);
Thread labelThread = new Thread(new ThreadStart(new MoveChar(lab,this).Move));
labelThread.Start();
}
private void button1_Click(object sender, System.EventArgs e)
{
if(timer1.Enabled==true)
{
timer1.Stop();
return;
}
if(timer1.Enabled==false)
{
timer1.Interval=500;
timer1.Start();
return;
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.BackColor = Color.Black;
}
private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
foreach (Label labeobj in this.Controls)
{
if (labeobj == null)
{
break;
}
else if (e.KeyChar.ToString() == labeobj.Text)
{
labeobj.Dispose();
//Thread.CurrentThread.Abort();
this.Controls.Remove(labeobj);
}
}
}
}
public class MoveChar
{
private Label labTeam;
private Form formlab;
public MoveChar(Label lab, Form form)
{
labTeam = lab;
formlab = form;
}
public void Move()
{
while (true)
{
Thread.Sleep(30);
labTeam.Invoke(new MethodInvoker(mm));
if (this.labTeam.Top >=this.formlab.Height)
{
this.labTeam.Invoke(new MethodInvoker(clr));
Thread.CurrentThread.Abort();
}
}
}
public void clr()
{
this.labTeam.Dispose();
}
public void mm()
{
this.labTeam.Top += 1;
}
}
}