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 ...
随机推荐
- qt 元对象系统
元对象系统 Qt中的元对象系统是用来处理对象间通讯的信号/槽机制.运行时的类型信息和 动态属性系统. 它基于下列三类: QObject类: 类声明中的私有段中的Q_OBJECT宏: 元对象编译器(mo ...
- 【VMWare】虚拟机启动遇到黑屏,在命令行窗口输入netsh winsock reset并不管用 重新启动客户机就好了
现象:虚拟机启动后是莫名其妙的黑屏,而且它上面安装的MySql也无法访问了. 处置:上网百度方案,看到大多数网文推荐:“以管理员身份打开cmd,输入netsh winsock reset,然后重启机器 ...
- 【论文学习】A Fuzzy-Rule-Based Approach for Single Frame Super Resolution
加尔各答印度统计研究所,作者: Pulak Purkait (pulak_r@isical.ac.in) 2013 年 代码:CodeForge.cn http://www.codeforge.cn/ ...
- 反射中的 Method 的 getReadMethod 与 getWriteMethod 使用 【获取一个对象的所有属性字段名称和其对应的值】
转: class反射(一),以及Method 的 getReadMethod 与 getWriteMethod 使用 2018年11月28日 17:27:42 zich77521 阅读数 788 ...
- VLAN和VXLAN的区别
VLAN ·概况 VLAN (Virtual Local Area Network)意为虚拟局域网,是在交换机实现过程中涉及到的概念,由802.1Q标准所定义.由于交换机是工作在链路层的网络设备,连接 ...
- iscsi序列二、iscsi多路径配置方式
一.ISCSI多路径应用 如果存储服务器到交换机只有一条线路的时候,那么一条线路出线故障,整个就没法使用了,所以多线路可以解决这个问题,避免单点故障 如上图,如果SAN服务器与客户端交换机只有一条线路 ...
- IDEA 2017 安装和破解
IDEA 2017 下载地址 链接:http://pan.baidu.com/s/1qXNa9UO 密码:9wwg 激活注册码:http://xidea.online 1-选择安装地址 2-选择安装的 ...
- delphi数据集查找不定位
procedure TForm1.Button2Click(Sender: TObject); var R: Variant; begin R := MemTableEh1.Lookup('Name' ...
- 1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
在数据库执行脚本文件时,执行到一半会遇到 这种问题: 出错处:2018-05-14 18:53:38 行号:712454 错误代码: 1293 - Incorrect table definitio ...
- SpringBoot: 5.访问静态资源(转)
springboot默认从项目的resources里面的static目录下或者webapp目录下访问静态资源 方式一:在resources下新建static文件(文件名必须是static) 在浏览器中 ...