模仿原本的WinForm触摸屏小键盘制作WPF触摸屏小键盘

原WinForm触摸屏小键盘样式(WinForm采用Krypton系列控件)如下图:

WinForm数字小键盘/WPF数字小键盘-LMLPHP

Designer代码如下:

             //
             // BtnNum1
             //
             this.BtnNum1.Dock = System.Windows.Forms.DockStyle.Fill;
             , );
             this.BtnNum1.Name = "BtnNum1";
             , );
             )));
             )));
             ;
             ";
             this.BtnNum1.Click += new System.EventHandler(this.BtnNum_Click);
             //
             // BtnNum2
             //
             this.BtnNum2.Dock = System.Windows.Forms.DockStyle.Fill;
             , );
             this.BtnNum2.Name = "BtnNum2";
             , );
             )));
             )));
             ;
             ";
             this.BtnNum2.Click += new System.EventHandler(this.BtnNum_Click);
             //
             // BtnNum3
             //
             this.BtnNum3.Dock = System.Windows.Forms.DockStyle.Fill;
             , );
             this.BtnNum3.Name = "BtnNum3";
             , );
             )));
             )));
             ;
             ";
             this.BtnNum3.Click += new System.EventHandler(this.BtnNum_Click);
             //
             // BtnNum4
             //
             this.BtnNum4.Dock = System.Windows.Forms.DockStyle.Fill;
             , );
             this.BtnNum4.Name = "BtnNum4";
             , );
             )));
             )));
             ;
             ";
             this.BtnNum4.Click += new System.EventHandler(this.BtnNum_Click);
             //
             // BtnNum5
             //
             this.BtnNum5.Dock = System.Windows.Forms.DockStyle.Fill;
             , );
             this.BtnNum5.Name = "BtnNum5";
             , );
             )));
             )));
             ;
             ";
             this.BtnNum5.Click += new System.EventHandler(this.BtnNum_Click);
             //
             // BtnNum6
             //
             this.BtnNum6.Dock = System.Windows.Forms.DockStyle.Fill;
             , );
             this.BtnNum6.Name = "BtnNum6";
             , );
             )));
             )));
             ;
             ";
             this.BtnNum6.Click += new System.EventHandler(this.BtnNum_Click);
             //
             // BtnNum7
             //
             this.BtnNum7.Dock = System.Windows.Forms.DockStyle.Fill;
             , );
             this.BtnNum7.Name = "BtnNum7";
             , );
             )));
             )));
             ;
             ";
             this.BtnNum7.Click += new System.EventHandler(this.BtnNum_Click);
             //
             // BtnNum8
             //
             this.BtnNum8.Dock = System.Windows.Forms.DockStyle.Fill;
             , );
             this.BtnNum8.Name = "BtnNum8";
             , );
             )));
             )));
             ;
             ";
             this.BtnNum8.Click += new System.EventHandler(this.BtnNum_Click);
             //
             // BtnNum9
             //
             this.BtnNum9.Dock = System.Windows.Forms.DockStyle.Fill;
             , );
             this.BtnNum9.Name = "BtnNum9";
             , );
             )));
             )));
             ;
             ";
             this.BtnNum9.Click += new System.EventHandler(this.BtnNum_Click);
             //
             // BtnNum0
             //
             );
             this.BtnNum0.Dock = System.Windows.Forms.DockStyle.Fill;
             , );
             this.BtnNum0.Name = "BtnNum0";
             , );
             )));
             )));
             ;
             ";
             this.BtnNum0.Click += new System.EventHandler(this.BtnNum_Click);
             //
             // BtnNumDel
             //
             this.BtnNumDel.Dock = System.Windows.Forms.DockStyle.Fill;
             , );
             this.BtnNumDel.Name = "BtnNumDel";
             , );
             )));
             )));
             ;
             this.BtnNumDel.Values.Text = "Del";
             this.BtnNumDel.Click += new System.EventHandler(this.BtnNumDel_Click);

所有按钮的点击事件为BtnNumDel_Click,功能实现的代码如下:

         #region 小键盘按钮事件
         /// <summary>
         /// 触摸小键盘
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void BtnNum_Click(object sender, EventArgs e)
         {
             KryptonButton btn = (KryptonButton)sender;
             try
             {
                 CurrentTextEdit.Text += btn.Text;
             }
             catch
             {

             }
         }
         /// <summary>
         /// 进入控件发生事件
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void textEdit_Enter(object sender, EventArgs e)
         {
             CurrentTextEdit = (KryptonTextBox)sender;
         }
         /// <summary>
         /// Del按钮
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void BtnNumDel_Click(object sender, EventArgs e)
         {
             CurrentTextEdit.Text = string.Empty;
         }

         #endregion

对于简易的小键盘来说,个人觉得这么写还算简洁。只要把想输入的TextBox的Enter事件设置为textEdit_Enter即可实现输入。

由于WPF初学,所以没有想到更简单的方法,而且WPF与WinForm很相似,所以仿照着写了一个。

XAML代码如下:

             <Button Content="1" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Margin="5" Click="BtnKeyBoard_Click"/>
             <Button Content="2" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Column="1" Margin="5" Click="BtnKeyBoard_Click"/>
             <Button Content="3" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Column="2" Margin="5" Click="BtnKeyBoard_Click"/>
             <Button Content="4" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Row="1" Margin="5" Click="BtnKeyBoard_Click"/>
             <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"/>
             <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"/>
             <Button Content="7" Style="{DynamicResource BtnS1}" FontFamily="KaiTi_GB2312" FontWeight="Bold" FontSize="29.333" Grid.Row="2" Margin="5" Click="BtnKeyBoard_Click"/>
             <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"/>
             <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"/>
             <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"/>
             <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事件代替,代码如下:

         #region 小键盘事件
         /// <summary>
         /// 触摸小键盘
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void BtnKeyBoard_Click(object sender, RoutedEventArgs e)
         {
             ///获取父类中的Btn
             Button btn = (Button)e.OriginalSource;
             if (pickbox == true)
             {
                 try
                 {
                     TextBoxEdit.Text += btn.Content;
                 }
                 catch
                 {

                 }
             }
             else if (pickbox == false)
             {
                 try
                 {
                     PasswordBoxEdit.Password += btn.Content;
                 }
                 catch
                 {

                 }
             }
         }
         /// <summary>
         /// 删除输入
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void Del_Click(object sender, EventArgs e)
         {
             if (pickbox == true)
             {
                 TextBoxEdit.Text = string.Empty;
             }
             else if (pickbox == false)
             {
                 PasswordBoxEdit.Password =string.Empty;
             }
         }
         /// <summary>
         /// 用户编号Focus
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void TextBoxEdit_Focus(object sender, EventArgs e)
         {
             TextBoxEdit = (TextBox)sender;
             pickbox = true;
         }
         /// <summary>
         /// 用户密码Focus
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void PasswordBoxEdit_Focus(object sender, EventArgs e)
         {
             PasswordBoxEdit = (PasswordBox)sender;
             pickbox = false;
         }
         #endregion

而且WPF的TextBox控件与PasswordBox控件不同,无奈之下还加了一个变量pickbox用来区分。

04-15 06:41