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 ...
随机推荐
- LeetCode205----同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一 ...
- Java通过过滤器修改header
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- oracle存储过程中进行分页
create or replace procedure APP_BUSSINESS_CARD_LIST(p_in_str in varchar2,p_out_str out varchar2) is ...
- 编译安装 Nginx
一.下载 https://nginx.org/en/download.html yum install -y wget wget http://nginx.org/download/nginx-1.1 ...
- ubuntu16.04下如何安装mkimage工具?
答: sudo apt-get install u-boot-tools -y
- ubuntu下如何使用dpkg删除某个软件包?
答:步骤如下: 1. 找出包名 dpkg -l |grep <package name> 2. 卸载 dpkg -r <package name>
- Hander创建消息
每一个消息都需要被指定的Handler处理,通过Handler创建消息便可以完成此功能.Android消息机制中引入了消息池.Handler创建消息时首先查询消息池中是否有消息存在,如果有直接从消息池 ...
- [GPU] Install H2O.ai
一.前言 主页:https://www.h2o.ai/products/h2o4gpu/ GPU版本安装:h2oai/h2o4gpu 采用GPU,能否成为超越下面链接中实验的存在? [ML] LIBS ...
- docker数据管理(2)
一.docker存储资源类型 docker两种存储资源类型 用户在使用 Docker 的过程中,势必需要查看容器内应用产生的数据,或者需要将容器内数据进行备份,甚至多个容器之间进行数据共享,这必然会涉 ...
- nginx的负载均衡和反向代理
本文介绍一些负载均衡和反向代理的一些基本概念,然后介绍如何基于nginx实现,包括两种安装nginx的方法:yum安装和源码安装,以及ngix该如何配置等. 什么是负载均衡? 概念 负载均衡是高可用网 ...