惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
P
Proofpoint News Feed
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
F
Fortinet All Blogs
S
Schneier on Security
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
I
InfoQ
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
Forbes - Security
Forbes - Security
Vercel News
Vercel News
S
Security Affairs
美团技术团队
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
U
Unit 42
Recorded Future
Recorded Future
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
P
Palo Alto Networks Blog
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 【当耐特】

博客园 - 段静迪

跟我学LINQ之一:为什么要学习LINQ C语言游戏 JAVA猜字游戏 在.NET平台下实现打印超市收银票据 JAVA上课源代码 基于三层架构的软件开发技术 MS-SQL数据库开发常用汇总 计算机类在Java中的设计于实现码 基于C语言的个人所得税计税系统 JAVA 教学实例 在.NET平台下使用SQL2000 Image类型数据 在.NET平台下使用SQL2000 Image类型数据 对SQL Server2000数据块进行查询 界面和业务代码分离的记事本 在.Net平台下读写文件流 多线程摇奖机程序源代码 网页第五章 HTML标签详解 JDK 目录下的*.exe文件的使用
键盘游戏
段静迪 · 2007-10-15 · via 博客园 - 段静迪

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
   {
   }
  }
 }
}