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 ...
随机推荐
- 打印li索引值
<ul> <li>这是第一条alert(0)</li> <li>这是第二条alert(1)</li> <li>这是第三条aler ...
- LC 979. Distribute Coins in Binary Tree
Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there ar ...
- js实现动态显示时间
思路: *得到当前时间 var date = new Date(); //格式化为本地时间 var d1 = date.toLocaleString(); *使页面每秒显示一次时间 setInterv ...
- H5和原生APP之间的区别
最近项目中因各种客观因素,移动端都是默认用的纯H5 APP,感受最深的就是各种坑啊,好大的坑啊.产品上线后,带着各种坑后的总结原因方发现很多人都说纯H5 APP一次编写就能支持android和IOS两 ...
- sparkstreaming的状态计算-updateStateByKey源码
转发请注明原创地址:https://www.cnblogs.com/dongxiao-yang/p/11358781.html 本文基于spark源码版本为2.4.3 在流式计算中通常会有状态计算的需 ...
- 007-Linux 查看端口
1.使用ss 查看 ss 一般用于转储套接字统计信息.它还可以显示所有类型的套接字统计信息,包括 PACKET.TCP.UDP.DCCP.RAW.Unix 域等. ss -lntpd | grep : ...
- springboot-多环境测试
1.application.properties中添加spring.profiles.active=test 2.同级目录下创建application-dev.properties.applicati ...
- Chrome浏览器界面截图
常常出现这么一个场景: 网页比较长,需要滚动屏幕才能看完整.这时候如需截屏,则比较麻烦. 如下为解决方法: 推荐Chrome浏览器: 按F12打开调试页面,同时按下ctrl + shift + p, ...
- 【052-week 预习周】学习总结
目录 学习手册 学习理念 学员精选 课前准备 概览 数据结构思维导图 算法思维导图 学习手册 学习理念 让优秀的人一起学习 师傅领进门,修行靠个人 学员精选 稻盛和夫"六项精进" ...
- CSS实现网页背景图片自适应全屏,自适应背景图片
一张清晰漂亮的背景图片能给网页加分不少,设计师也经常会给页面的背景使用大图,我们既不想图片因为不同分辨率图片变形,也不希望当在大屏的情况下,背景有一块露白,简而言之,就是实现能自适应屏幕大小又不会变形 ...