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 ...
随机推荐
- No hash for parcel CDH-XXX.parcel.torrent
在安装 CDH 时,到 Install Parcels 这一步,分发 Parcels 一直过不去,界面一直报 Failure due to stall on seeded torrent,查看日志一直 ...
- LC 835. Image Overlap
Two images A and B are given, represented as binary, square matrices of the same size. (A binary ma ...
- JavaScript中字符串,数组的基本操作
JavaScript的字符串就是用”或”“括起来的字符表示. js中操作字符串: 1.获得字符串的长度 var s = 'Hello, world!'; s.length; // 132.获取指定字符 ...
- 数据分析 - matpltlib 模块
matplotlib 模块 引入模块 import matplotlib.pyplot as plt 设置图片大小 - figure 展示图片 - show 画图 - 实例化后指定类型画图 plot ...
- Python3+RobotFramewok 用户自定义库的开发(四)
在介绍这个之前,可以先看下python的目录Python\Lib\site-packages下面的文件夹,你会发现这个目录下面有DatabaseLibrary.RequestsLibrary.Sele ...
- vue指令实现拖动的高级写法
不熟悉vue自定义指令看这里: https://cn.vuejs.org/v2/guide/custom-directive.html vue指令实现拖动方法很方便也挺简单,但是网上大部分的教程代码, ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_5-06 高级篇幅之深入源码
笔记 6.高级篇幅之深入源码剖析Hystrix降级策略和调整 简介:源码分析Hystrix降级策略和调整 1.查看默认讲解策略 HystrixCommandProperties ...
- [PySpark] RDD programming on a large file
重难点 一.parallelize 方法 一般来说,Spark会尝试根据集群的状况,来自动设定slices的数目.然而,你也可以通过传递给parallelize的第二个参数来进行手动设置. data_ ...
- IPv6 ping命令
IPv6 ping命令 一.Linux操作系统 给一台 Linux 主机分配了一个 IPv6 的 IP地址,如何使用 ping命令 确定该 IP地址 能否 ping 通呢? 1.查看主机的 IPv6 ...
- java使用nio(Paths,Files)遍历文件目录,转成java.io.File
String directory = "C:\\Users\\Administrator\\AppData\\Local\\Temp\\8ad088a2-0bb3-41dc-89d9-2c5 ...