#指法练习,编写中文练习的人也可参考 #BeJon http://topic.csdn.net/u/20080711/15/829c4155-f35a-41b2-a2ca-a8253f48c323.html using System; using System.Text.RegularExpressions; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace 指法 { public partial class Form1 : Form { public class sLabel : Label { public sLabel() { this.Font = new Font("Courier New", 15); this.Anchor = AnchorStyles.Left | AnchorStyles.Right; this.Visible = false; this.AutoSize = true; this.Height = 27; this.BorderStyle = BorderStyle.None; } } public class sRichtextbox : RichTextBox { public sRichtextbox() { this.Font = new Font("Courier New", 15); this.Height = 27; this.ForeColor = Color.Black; this.Anchor = AnchorStyles.Left | AnchorStyles.Right; this.Visible = false; this.ScrollBars = RichTextBoxScrollBars.None; } } public Form1() { InitializeComponent(); this.MinimumSize = new Size(597, 405); this.StartPosition = FormStartPosition.CenterScreen; } List<Label> lstLable = new List<Label>(); List<RichTextBox> lstRichTextBox = new List<RichTextBox>(); int NowWorldLengt = 0; private void Form1_Resize(object sender, EventArgs e) { int intWidth = this.ClientSize.Width; bool bVisible = false; for (int i = 0; i < 30; i++) { int y = i * 55 +70; //27为toolstrip高度 if (y + 5 > this.ClientSize.Height) { ShowRichtextboxCount = i; bVisible = false; } else bVisible = true; lstLable[i].Location = new Point(0, y-45); lstLable[i].Width = intWidth; lstLable[i].Visible = bVisible; lstRichTextBox[i].Location = new Point(0,y-22); lstRichTextBox[i].Width = intWidth; lstRichTextBox[i].Visible = bVisible; } switch (iCharacter) { case 1: a_z(null, null); break; case 2: A_z(null, null); break; case 3: A_z_Num(null, null); break; } } Timer timer = new Timer(); //计时器 ToolStripLabel tsLSpeed = new ToolStripLabel(); int iCharacter = 0; private void Form1_Load(object sender, EventArgs e) { for (int i = 0; i < 30; i++) { sLabel a = new sLabel(); sRichtextbox b = new sRichtextbox(); lstLable.Add(a); lstRichTextBox.Add(b); this.Controls.Add(a); this.Controls.Add(b); b.Tag = i; b.TextChanged += inputboxTextChange; b.SelectionChanged += inputboxSelectionChange; b.KeyDown += inputboxKeyDown; b.GotFocus += inputFocus; } ToolStrip ts = new ToolStrip(); ToolStripButton tb1; tb1=AddNewTsB("纯小写字母训练", ref ts); tb1.Click += a_z; tb1 = AddNewTsB("大小写字母训练", ref ts); tb1.Click += A_z; tb1 = AddNewTsB("字母及数字训练", ref ts); tb1.Click += A_z_Num; ToolStripSeparator tss1 = new ToolStripSeparator(); ts.Items.Add(tss1); AddNewTsB("当前速度", ref ts); ts.Items.Add(tsLSpeed); this.Controls.Add(ts); timer.Interval = 1000; timer.Tick += CountTime; Form1_Resize(null,null); } ToolStripButton AddNewTsB(string strText,ref ToolStrip ts) { ToolStripButton tb1=new ToolStripButton(); tb1.Text = strText; tb1.DisplayStyle = ToolStripItemDisplayStyle.Text; ts.Items.Add(tb1); return tb1; } int RightKey = 0; //正确字符数量 int WrongKey = 0; //错误字符数量 int ElapseTime = 0;//使用时间数量(秒) void a_z(object sender, EventArgs e) { getText('a', 'z'); iCharacter = 1; } void A_z(object sender, EventArgs e) { getText('A', 'z'); iCharacter = 2; } void A_z_Num(object sender, EventArgs e) { getText('0', 'z'); iCharacter = 3; } void getText(int miniValue, int maxValue) { //计算当前看得到的字符 NowWorldLengt = (this.ClientSize.Width - 10) / 12; ElapseTime = 0; RightKey = 0; WrongKey = 0; timer.Enabled = true; int iTextLength = 0; Random rd = new Random(Guid.NewGuid().GetHashCode()); Regex reg = new Regex(@"(/d|/w)"); Regex reg2 = new Regex(@"/w"); for (int l1 = 0; l1 < 30; l1++) { StringBuilder sb = new StringBuilder(); while (iTextLength < NowWorldLengt) { char c = (char)rd.Next(miniValue, maxValue); if (miniValue == '0') { if (reg.Match(c.ToString()).Success && c!='_') { sb.Append(c); iTextLength++; Application.DoEvents(); } } else { if (reg2.Match(c.ToString()).Success && c != '_') { sb.Append(c); iTextLength++; } } } iTextLength = 0; lstLable[l1].Text = sb.ToString(); lstRichTextBox[l1].Clear(); } lstRichTextBox[0].Focus(); } void CountTime(object sender, EventArgs e) { ElapseTime++; tsLSpeed.Text = "正确:" + RightKey.ToString() + " 错误:" + WrongKey.ToString() + " 费时:" + ElapseTime.ToString()+" 速度:" +(RightKey*60/ElapseTime).ToString()+"/分钟"; } int NowIndex = -1; //当前正在输入的richtextbox void inputFocus(object sender, EventArgs e) { RichTextBox b=(RichTextBox) sender; NowIndex = (int)b.Tag; } void inputboxKeyDown(object sender, KeyEventArgs e) { if (e.Control || e.KeyValue==16) return; RichTextBox rb=(RichTextBox) sender; int iDiff = 32; if (Control.IsKeyLocked(Keys.CapsLock) && e.Shift) iDiff *= -1; else { if (e.Shift || Control.IsKeyLocked(Keys.CapsLock)) iDiff = 0; } if (e.KeyValue + iDiff == lstLable[NowIndex].Text[rb.SelectionStart]) { rb.SelectionColor = Color.Black; RightKey++; } else { rb.SelectionColor = Color.Red; WrongKey++; } } void inputboxSelectionChange(object sender, EventArgs e) { RichTextBox rt = (RichTextBox)sender; rt.SelectionStart = rt.Text.Length; } int ShowRichtextboxCount = -1; //一共显示出来的输入框数 void inputboxTextChange(object sender, EventArgs e) { if (lstRichTextBox[NowIndex].Text.Length == NowWorldLengt) { ++NowIndex; if (NowIndex == ShowRichtextboxCount) { timer.Enabled = false; if (MessageBox.Show("恭喜你完成本次练习!", "恭喜", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { switch (iCharacter) { case 1: a_z(null, null); break; case 2: A_z(null, null); break; case 3: A_z_Num(null, null); break; } } return; } lstRichTextBox[NowIndex].Focus(); } } } }