Winform自定义键盘控件开发及使用
最近有学员提出项目中要使用键盘控件,系统自带的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 = ,
NUMBER =
}
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);
}
}
} }
}
实际应用效果如下:

Winform自定义键盘控件开发及使用的更多相关文章
- C#自定义工业控件开发
由于工作需要,调研过一段时间的工业控制方面的“组态软件”(SCADA)的开发,组态软件常用于自动化工业控制领域,其中包括实时数据采集.数据储存.设备控制和数据展现等功能.其中工控组件的界面展现的实现类 ...
- WinForm自定义验证控件
本文转载:http://blog.csdn.net/ziyouli/article/details/7583824 此篇博文不错:http://blog.csdn.net/sony0732/artic ...
- 在IE中点击转跳,并打开chorme浏览器继续浏览指定页面,IE自定义ocx控件开发
因项目需要,需要开发一个功能:在IE中点击转跳,并打开chorme浏览器继续浏览指定页面. 分析需求后,参考了: https://www.cnblogs.com/ffjiang/p/7908025.h ...
- Winform自定义分页控件的实现
实现效果 有点丑陋 但是功能是没问题的 测试过 实现思路 先创建一个用户控件 代码实现 public partial class PagerControl : UserControl { ; /// ...
- winform 自定义分页控件 及DataGridview数据绑定
分页效果如上图所示,用到的控件均为基本控件 ,其方法如下 右击项目-添加-新建项 选择用户控件 然后在用户控件中拖入所需要的Label,Button,Text 用户控件全部代码: using Syst ...
- winform自定义分页控件
1.控件代码: public partial class PagerControl : UserControl { #region 构造函数 public PagerControl() { Initi ...
- 自定义select控件开发
目的:select下拉框条目太多(上百),当用户选择具体项时会浪费用户很多时间去寻找,因此需要一个搜索框让用户输入关键字来匹配列表,便于用户选择 示例图: 1.html结构 <div class ...
- winform自定义日期控件,要求可以手动输入日期DatePicker
要求:文本框中能手动输入数字,向上箭头根据鼠标位置给年月日递增,向下箭头递减 一:页面加载时: private void FlatDatePicker_Load(object sender, Even ...
- C# winform自定义Label控件使其能设置行距
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
随机推荐
- 码支付(php版本)应用
1.下载软件 2.安装php 版本 2.打开软件,登陆支付宝 3.codepay_config.php 中填写逻辑代码 4.充值路径: 说明:$username = trim($_GET['user ...
- 从char到QChar
char类型是c/c++中内置的类型,描述了1个字节的内存信息的解析.比如: char gemfield=’g’; 那么在由gemfield标记的这块内存的大小就是1个字节,信息就是01100111, ...
- CNN中感受野大小的计算
1 感受野的概念 从直观上讲,感受野就是视觉感受区域的大小.在卷积神经网络中,感受野的定义是 卷积神经网络每一层输出的特征图(feature map)上的像素点在原始图像上映射的区域大小. 2 感受野 ...
- Java面试题整理(转载)
转载地址:https://blog.csdn.net/matry521/article/details/52210139
- ios 报错记录
1. 运行xcode 报错:unterminated conditional directive #ifdef 缺少对应的#endif 在结尾加上就好了 2.iOS添加非(c,c++)文件引发的&qu ...
- CSS 背景色变化 结构化伪类的练习
CSS3的nth-child() 选择器(兼容性不好),在做表格偶数行变色的时候,我通常在绑定的时候,做一个js判断,来加一个css,从而使表格偶数行和奇数行颜色不一样.这样的兼容性很好. 但是最近在 ...
- 10 MySQL之数据备份与恢复
01-数据备份 # 1.使用 MySQLdump 命令备份 MySQLdump 是MySQL提供的一个非常有用的数据库备份工具. MySQLdump 命令执行时,可以将数据库备份成一个文本文件,该文本 ...
- 006-top查看计算机信息
1.使用top命令基本信息 us:用户态使用的cpu时间比sy:系统态使用的cpu时间比ni:用做nice加权的进程分配的用户态cpu时间比id:空闲的cpu时间比wa:cpu等待磁盘写入完成时间hi ...
- jsp细节------<base>
1:jsp一般都有这个<base href="<%=basePath%>">,它的作用一般用不到,但在使用java框架用注解时会用. 如下代码(xxx.js ...
- Linux文档中翻页和搜索关键字
按键 进行工作空格键 向下翻一页[Page Down] 向下翻一页[Page Up] 向上翻一页[Ctrl + U] 向上翻一页[Ctrl + D] 向下翻一页/string 向下搜寻string这个 ...