最近有学员提出项目中要使用键盘控件,系统自带的osk.exe不好用,于是就有了下面的内容:
首先是进行自定义键盘控件的开发,其实核心大家都知道,就是利用SendKeys.Send发送相应
的字符,但是为了做完整,还是加了一些其他的代码,具体样式如下图所示:
源码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace xktControl { public enum KeyBorderCharType { CHAR = 1, NUMBER = 2 } public partial class xktKeyBoard : UserControl { public xktKeyBoard() { InitializeComponent(); this.tableLayoutPanel2.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single; EventHandle(this); } private void EventHandle(Control ctl) { foreach (Control item in ctl.Controls) { if (item is Label lb) { lb.MouseDown += KeyDown_MouseDown; } else if (item.HasChildren) { EventHandle(item); } } } [Browsable(true), Description("按键点击事件"), Category("自定义属性")] public event EventHandler KeyClick; [Browsable(true), Description("回车点击事件"), Category("自定义属性")] public event EventHandler EnterClick; /// <summary> /// Occurs when [backspace clike]. /// </summary> [Browsable(true), Description("删除点击事件"), Category("自定义属性")] public event EventHandler BackspaceClick; /// <summary> /// Occurs when [retract clike]. /// </summary> [Browsable(true), Description("关闭点击事件"), Category("自定义属性")] public event EventHandler CloseClick; private void KeyDown_MouseDown(object sender, MouseEventArgs e) { if (sender is Label lbl) { if (string.IsNullOrEmpty(lbl.Text)) { return; } if (lbl.Text == "CAP") { ToUpperOrLower(this,true); lbl.Text = "cap"; } else if (lbl.Text == "cap") { ToUpperOrLower(this,false); lbl.Text = "CAP"; } else if (lbl.Text == "?123" || lbl.Text == "abc.") { ChangeShow(this); } else if (lbl.Text == "空格") { SendKeys.Send(" "); } else if (lbl.Text.ToLower() == "shift") { SendKeys.Send("+"); if (lbl.Text == "shift") { lbl.Text = "SHIFT"; lbl.Tag = "SHIFT"; } else { lbl.Text = "shift"; lbl.Tag = "shift"; } } else if (lbl.Text == "删除") { SendKeys.Send("{BACKSPACE}"); BackspaceClick?.Invoke(sender, e); } else if (lbl.Text == "回车") { SendKeys.Send("{ENTER}"); EnterClick?.Invoke(sender, e); } else if (lbl.Text == "关闭") { CloseClick?.Invoke(this, e); } else { string Str = "{" + lbl.Text + "}"; SendKeys.Send(lbl.Text); KeyClick?.Invoke(sender, e); } } } private KeyBorderCharType charType = KeyBorderCharType.CHAR; [Browsable(true), Description("显示样式"), Category("自定义属性")] public KeyBorderCharType CharType { get { return charType; } set { charType = value; if (value == KeyBorderCharType.CHAR) { if (lbl_NumChar.Text.ToLower() == "abc.") { ChangeShow(this); } } else { if (lbl_NumChar.Text.ToLower() == "?123") { ChangeShow(this); } } } } private void ToUpperOrLower(Control ctl, bool bln) { foreach (Control item in ctl.Controls) { if (item is Label lbl) { if (lbl.Text == "abc." || lbl.Text.ToLower() == "shift") return; lbl.Text = bln ? lbl.Text.ToUpper() : lbl.Text.ToLower(); } else if (item.HasChildren) { ToUpperOrLower(item, bln); } } } private void ChangeShow(Control ctl) { foreach (Control item in ctl.Controls) { if (item is Label lb) { string strTag = lb.Text; lb.Text = lb.Tag.ToString(); lb.Tag = strTag; } else if (item.HasChildren) { ChangeShow(item); } } } } }
实际应用效果如下: