模仿原本的WinForm触摸屏小键盘制作WPF触摸屏小键盘
原WinForm触摸屏小键盘样式(WinForm采用Krypton系列控件)如下图:
Designer代码如下:
1 // 2 // BtnNum1 3 // 4 this.BtnNum1.Dock = System.Windows.Forms.DockStyle.Fill; 5 this.BtnNum1.Location = new System.Drawing.Point(3, 3); 6 this.BtnNum1.Name = "BtnNum1"; 7 this.BtnNum1.Size = new System.Drawing.Size(125, 62); 8 this.BtnNum1.StateCommon.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 9 this.BtnNum1.StateNormal.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 10 this.BtnNum1.TabIndex = 0; 11 this.BtnNum1.Values.Text = "1"; 12 this.BtnNum1.Click += new System.EventHandler(this.BtnNum_Click); 13 // 14 // BtnNum2 15 // 16 this.BtnNum2.Dock = System.Windows.Forms.DockStyle.Fill; 17 this.BtnNum2.Location = new System.Drawing.Point(134, 3); 18 this.BtnNum2.Name = "BtnNum2"; 19 this.BtnNum2.Size = new System.Drawing.Size(126, 62); 20 this.BtnNum2.StateCommon.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 21 this.BtnNum2.StateNormal.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 22 this.BtnNum2.TabIndex = 1; 23 this.BtnNum2.Values.Text = "2"; 24 this.BtnNum2.Click += new System.EventHandler(this.BtnNum_Click); 25 // 26 // BtnNum3 27 // 28 this.BtnNum3.Dock = System.Windows.Forms.DockStyle.Fill; 29 this.BtnNum3.Location = new System.Drawing.Point(266, 3); 30 this.BtnNum3.Name = "BtnNum3"; 31 this.BtnNum3.Size = new System.Drawing.Size(127, 62); 32 this.BtnNum3.StateCommon.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 33 this.BtnNum3.StateNormal.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 34 this.BtnNum3.TabIndex = 2; 35 this.BtnNum3.Values.Text = "3"; 36 this.BtnNum3.Click += new System.EventHandler(this.BtnNum_Click); 37 // 38 // BtnNum4 39 // 40 this.BtnNum4.Dock = System.Windows.Forms.DockStyle.Fill; 41 this.BtnNum4.Location = new System.Drawing.Point(3, 71); 42 this.BtnNum4.Name = "BtnNum4"; 43 this.BtnNum4.Size = new System.Drawing.Size(125, 62); 44 this.BtnNum4.StateCommon.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 45 this.BtnNum4.StateNormal.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 46 this.BtnNum4.TabIndex = 3; 47 this.BtnNum4.Values.Text = "4"; 48 this.BtnNum4.Click += new System.EventHandler(this.BtnNum_Click); 49 // 50 // BtnNum5 51 // 52 this.BtnNum5.Dock = System.Windows.Forms.DockStyle.Fill; 53 this.BtnNum5.Location = new System.Drawing.Point(134, 71); 54 this.BtnNum5.Name = "BtnNum5"; 55 this.BtnNum5.Size = new System.Drawing.Size(126, 62); 56 this.BtnNum5.StateCommon.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 57 this.BtnNum5.StateNormal.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 58 this.BtnNum5.TabIndex = 4; 59 this.BtnNum5.Values.Text = "5"; 60 this.BtnNum5.Click += new System.EventHandler(this.BtnNum_Click); 61 // 62 // BtnNum6 63 // 64 this.BtnNum6.Dock = System.Windows.Forms.DockStyle.Fill; 65 this.BtnNum6.Location = new System.Drawing.Point(266, 71); 66 this.BtnNum6.Name = "BtnNum6"; 67 this.BtnNum6.Size = new System.Drawing.Size(127, 62); 68 this.BtnNum6.StateCommon.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 69 this.BtnNum6.StateNormal.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 70 this.BtnNum6.TabIndex = 5; 71 this.BtnNum6.Values.Text = "6"; 72 this.BtnNum6.Click += new System.EventHandler(this.BtnNum_Click); 73 // 74 // BtnNum7 75 // 76 this.BtnNum7.Dock = System.Windows.Forms.DockStyle.Fill; 77 this.BtnNum7.Location = new System.Drawing.Point(3, 139); 78 this.BtnNum7.Name = "BtnNum7"; 79 this.BtnNum7.Size = new System.Drawing.Size(125, 62); 80 this.BtnNum7.StateCommon.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 81 this.BtnNum7.StateNormal.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 82 this.BtnNum7.TabIndex = 6; 83 this.BtnNum7.Values.Text = "7"; 84 this.BtnNum7.Click += new System.EventHandler(this.BtnNum_Click); 85 // 86 // BtnNum8 87 // 88 this.BtnNum8.Dock = System.Windows.Forms.DockStyle.Fill; 89 this.BtnNum8.Location = new System.Drawing.Point(134, 139); 90 this.BtnNum8.Name = "BtnNum8"; 91 this.BtnNum8.Size = new System.Drawing.Size(126, 62); 92 this.BtnNum8.StateCommon.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 93 this.BtnNum8.StateNormal.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 94 this.BtnNum8.TabIndex = 7; 95 this.BtnNum8.Values.Text = "8"; 96 this.BtnNum8.Click += new System.EventHandler(this.BtnNum_Click); 97 // 98 // BtnNum9 99 // 100 this.BtnNum9.Dock = System.Windows.Forms.DockStyle.Fill; 101 this.BtnNum9.Location = new System.Drawing.Point(266, 139); 102 this.BtnNum9.Name = "BtnNum9"; 103 this.BtnNum9.Size = new System.Drawing.Size(127, 62); 104 this.BtnNum9.StateCommon.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 105 this.BtnNum9.StateNormal.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 106 this.BtnNum9.TabIndex = 8; 107 this.BtnNum9.Values.Text = "9"; 108 this.BtnNum9.Click += new System.EventHandler(this.BtnNum_Click); 109 // 110 // BtnNum0 111 // 112 this.tplKeyboard.SetColumnSpan(this.BtnNum0, 2); 113 this.BtnNum0.Dock = System.Windows.Forms.DockStyle.Fill; 114 this.BtnNum0.Location = new System.Drawing.Point(3, 207); 115 this.BtnNum0.Name = "BtnNum0"; 116 this.BtnNum0.Size = new System.Drawing.Size(257, 64); 117 this.BtnNum0.StateCommon.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 118 this.BtnNum0.StateNormal.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 119 this.BtnNum0.TabIndex = 9; 120 this.BtnNum0.Values.Text = "0"; 121 this.BtnNum0.Click += new System.EventHandler(this.BtnNum_Click); 122 // 123 // BtnNumDel 124 // 125 this.BtnNumDel.Dock = System.Windows.Forms.DockStyle.Fill; 126 this.BtnNumDel.Location = new System.Drawing.Point(266, 207); 127 this.BtnNumDel.Name = "BtnNumDel"; 128 this.BtnNumDel.Size = new System.Drawing.Size(127, 64); 129 this.BtnNumDel.StateCommon.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 130 this.BtnNumDel.StateNormal.Content.ShortText.Font = new System.Drawing.Font("隶书", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 131 this.BtnNumDel.TabIndex = 11; 132 this.BtnNumDel.Values.Text = "Del"; 133 this.BtnNumDel.Click += new System.EventHandler(this.BtnNumDel_Click);
所有按钮的点击事件为BtnNumDel_Click,功能实现的代码如下:
1 #region 小键盘按钮事件 2 /// <summary> 3 /// 触摸小键盘 4 /// </summary> 5 /// <param name="sender"></param> 6 /// <param name="e"></param> 7 private void BtnNum_Click(object sender, EventArgs e) 8 { 9 KryptonButton btn = (KryptonButton)sender; 10 try 11 { 12 CurrentTextEdit.Text += btn.Text; 13 } 14 catch 15 { 16 17 } 18 } 19 /// <summary> 20 /// 进入控件发生事件 21 /// </summary> 22 /// <param name="sender"></param> 23 /// <param name="e"></param> 24 private void textEdit_Enter(object sender, EventArgs e) 25 { 26 CurrentTextEdit = (KryptonTextBox)sender; 27 } 28 /// <summary> 29 /// Del按钮 30 /// </summary> 31 /// <param name="sender"></param> 32 /// <param name="e"></param> 33 private void BtnNumDel_Click(object sender, EventArgs e) 34 { 35 CurrentTextEdit.Text = string.Empty; 36 } 37 38 #endregion
对于简易的小键盘来说,个人觉得这么写还算简洁。只要把想输入的TextBox的Enter事件设置为textEdit_Enter即可实现输入。
由于WPF初学,所以没有想到更简单的方法,而且WPF与WinForm很相似,所以仿照着写了一个。
XAML代码如下:
1 <Button Content="1" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Margin="5" Click="BtnKeyBoard_Click"/> 2 <Button Content="2" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Column="1" Margin="5" Click="BtnKeyBoard_Click"/> 3 <Button Content="3" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Column="2" Margin="5" Click="BtnKeyBoard_Click"/> 4 <Button Content="4" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Row="1" Margin="5" Click="BtnKeyBoard_Click"/> 5 <Button Content="5" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Column="1" Grid.Row="1" Margin="5" Click="BtnKeyBoard_Click"/> 6 <Button Content="6" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Column="2" Grid.Row="1" Margin="5" Click="BtnKeyBoard_Click"/> 7 <Button Content="7" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Row="2" Margin="5" Click="BtnKeyBoard_Click"/> 8 <Button Content="8" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Column="1" Grid.Row="2" Margin="5" Click="BtnKeyBoard_Click"/> 9 <Button Content="9" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Column="2" Grid.Row="2" Margin="5" Click="BtnKeyBoard_Click"/> 10 <Button Content="0" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Row="3" Grid.ColumnSpan="2" Margin="5" Click="BtnKeyBoard_Click"/> 11 <Button Content="DEL" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Column="2" Grid.Row="3" Margin="5" Click="Del_Click"/>
这里WPF略有不同的是,TextBox并没有Enter事件,尝试多次使用GotFocus事件代替,代码如下:
1 #region 小键盘事件 2 /// <summary> 3 /// 触摸小键盘 4 /// </summary> 5 /// <param name="sender"></param> 6 /// <param name="e"></param> 7 private void BtnKeyBoard_Click(object sender, RoutedEventArgs e) 8 { 9 ///获取父类中的Btn 10 Button btn = (Button)e.OriginalSource; 11 if (pickbox == true) 12 { 13 try 14 { 15 TextBoxEdit.Text += btn.Content; 16 } 17 catch 18 { 19 20 } 21 } 22 else if (pickbox == false) 23 { 24 try 25 { 26 PasswordBoxEdit.Password += btn.Content; 27 } 28 catch 29 { 30 31 } 32 } 33 } 34 /// <summary> 35 /// 删除输入 36 /// </summary> 37 /// <param name="sender"></param> 38 /// <param name="e"></param> 39 private void Del_Click(object sender, EventArgs e) 40 { 41 if (pickbox == true) 42 { 43 TextBoxEdit.Text = string.Empty; 44 } 45 else if (pickbox == false) 46 { 47 PasswordBoxEdit.Password =string.Empty; 48 } 49 } 50 /// <summary> 51 /// 用户编号Focus 52 /// </summary> 53 /// <param name="sender"></param> 54 /// <param name="e"></param> 55 private void TextBoxEdit_Focus(object sender, EventArgs e) 56 { 57 TextBoxEdit = (TextBox)sender; 58 pickbox = true; 59 } 60 /// <summary> 61 /// 用户密码Focus 62 /// </summary> 63 /// <param name="sender"></param> 64 /// <param name="e"></param> 65 private void PasswordBoxEdit_Focus(object sender, EventArgs e) 66 { 67 PasswordBoxEdit = (PasswordBox)sender; 68 pickbox = false; 69 } 70 #endregion
而且WPF的TextBox控件与PasswordBox控件不同,无奈之下还加了一个变量pickbox用来区分。