using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace MoveChar
{
/// <summary>
/// frmMoveChar 的摘要说明。
/// </summary>
public class frmMoveChar : System.Windows.Forms.Form
{
private System.Windows.Forms.Button bntStart;
private System.Windows.Forms.Button bntExit;
private System.Windows.Forms.Timer timerStart;
private System.Windows.Forms.Label lblChar;
private System.Windows.Forms.Panel pnlContainer;
private System.ComponentModel.IContainer components;
public frmMoveChar()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// 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.bntStart = new System.Windows.Forms.Button();
this.bntExit = new System.Windows.Forms.Button();
this.timerStart = new System.Windows.Forms.Timer(this.components);
this.pnlContainer = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// bntStart
//
this.bntStart.Location = new System.Drawing.Point(384, 24);
this.bntStart.Name = "bntStart";
this.bntStart.Size = new System.Drawing.Size(88, 24);
this.bntStart.TabIndex = 1;
this.bntStart.Text = "开始";
this.bntStart.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.bntStart_KeyPress);
this.bntStart.Click += new System.EventHandler(this.bntStart_Click);
//
// bntExit
//
this.bntExit.Location = new System.Drawing.Point(384, 64);
this.bntExit.Name = "bntExit";
this.bntExit.Size = new System.Drawing.Size(88, 24);
this.bntExit.TabIndex = 2;
this.bntExit.Text = "退出";
this.bntExit.Click += new System.EventHandler(this.bntExit_Click);
//
// timerStart
//
this.timerStart.Tick += new System.EventHandler(this.timerStart_Tick);
//
// pnlContainer
//
this.pnlContainer.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.pnlContainer.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pnlContainer.Location = new System.Drawing.Point(8, 8);
this.pnlContainer.Name = "pnlContainer";
this.pnlContainer.Size = new System.Drawing.Size(368, 432);
this.pnlContainer.TabIndex = 3;
//
// frmMoveChar
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(480, 453);
this.Controls.Add(this.pnlContainer);
this.Controls.Add(this.bntExit);
this.Controls.Add(this.bntStart);
this.Name = "frmMoveChar";
this.Text = "frmMoveChar";
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.frmMoveChar_KeyPress);
this.Load += new System.EventHandler(this.frmMoveChar_Load);
this.ResumeLayout(false);
}
#endregion
private void frmMoveChar_Load(object sender, System.EventArgs e)
{
}
#region timer 控制 label 产生
private void timerStart_Tick(object sender, System.EventArgs e)
{
Random rand = new Random(System.DateTime.Now.Second);
char ch = (char)rand.Next(65,90);
string str = ch.ToString();
int x = rand.Next(0,280);
//
// lblChar
//
this.lblChar = new Label();
this.lblChar.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
this.lblChar.Font = new System.Drawing.Font("宋体", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.lblChar.Location = new System.Drawing.Point(x, 0);
this.lblChar.Name = "lblChar";
this.lblChar.Size = new System.Drawing.Size(20, 20);
this.lblChar.Text = str;
this.lblChar.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.pnlContainer.Controls.Add(this.lblChar);
this.lblChar.Visible = true;
Thread objThread = new Thread(new ThreadStart(new MoveChar(this.lblChar).Move));
objThread.Start();
}
#endregion
// 开始打字练习
private void bntStart_Click(object sender, System.EventArgs e)
{
this.timerStart.Enabled = true;
this.timerStart.Interval = 1000;
this.Focus();
}
#region 移动 label 和删除 label ,销毁线程
class MoveChar
{
Label lblTemp;
public MoveChar(Label lbl)
{
this.lblTemp = lbl;
}
public void Move()
{
while(true)
{
Thread.Sleep(100);
this.lblTemp.Top += 5;
if(this.lblTemp.Top >= 380)
{
this.lblTemp.Visible = false;
this.lblTemp.Dispose();
Thread.CurrentThread.Abort();
break;
}
}
}
}
#endregion
// 程序入口
public static void Main()
{
Application.Run(new frmMoveChar());
}
// 退出
private void bntExit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void frmMoveChar_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
}
// 接受用户键盘输入
private void bntStart_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
foreach(Label lbl in this.pnlContainer.Controls)
{
if(e.KeyChar.ToString().ToUpper().Equals(lbl.Text))
{
lbl.Visible = false;
lbl.Top = 390;
break;
}
}
}
}
}
浙公网安备 33010602011771号