标题:[求助]为什么键盘事件无效?
只看楼主
xxp5310776
Rank: 1
来 自:湖北武汉
等 级:新手上路
帖 子:49
专家分:1
注 册:2006-11-19
结帖率:100%
 问题点数:0 回复次数:0 
[求助]为什么键盘事件无效?

这是我写的一个打字小游戏,程序能运行,但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;
}
}

}

搜索更多相关主题的帖子: using System 键盘 Forms summary 
2007-06-20 18:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-149037-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.178453 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved