博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# Winform制作虚拟键盘,支持中文
阅读量:6580 次
发布时间:2019-06-24

本文共 7912 字,大约阅读时间需要 26 分钟。

原文:

          最近在做一个虚拟键盘功能,代替鼠标键盘操作,效果如下:

       实现思路:

         1  构建中文-拼音 数据库,我用的是SQLite数据库,如

              

         2 构建布局,如效果图

代码:

  数据库代码文件  SqlHandler.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.SQLite;using System.Configuration;using System.IO;using System.Reflection;using System.Windows.Forms;namespace TestKeyBord{     public class SqlHandler    {        public  static void InitSQLite(string db,string table)        {            try            {                DbName = db;                TableName = table;                if (CreateDataBase())                {                    _SQLiteCommand = _SQLiteConn.CreateCommand();                    _SQLiteCommand.Connection = _SQLiteConn;                    DesignerTable();                }            }            catch            {                          }        }        public static System.Data.ConnectionState SqliteState        {            get { return _SQLiteConn.State; }        }        #region 数据成员定义                public static string DbName = "MedicalSystemLog";        public static string TableName = "MedicalLog";        public static string _SQLiteConnString = string.Empty;        public static SQLiteConnection _SQLiteConn = new SQLiteConnection();               public static SQLiteCommand _SQLiteCommand = new SQLiteCommand();                             #endregion        #region 创建数据库文件        public static bool CreateDataBase()        {            try            {                _SQLiteConnString = "Data Source=" + DbName + ".db";                _SQLiteConn = new SQLiteConnection(_SQLiteConnString);                _SQLiteConn.Open();                _SQLiteCommand = _SQLiteConn.CreateCommand();                _SQLiteCommand.Connection = _SQLiteConn;                if (File.Exists(DbName + ".db"))                {                    return true;                }            }            catch            {               // MessageBox.Show("日志系统加载失败!");            }            return false;        }        #endregion        ///         /// 矩阵是否连接        ///         public static bool MatrixIsConnected = false;        #region 创建表         public static void DesignerTable()        {            try            {                if (_SQLiteConn.State != System.Data.ConnectionState.Open)                {                    _SQLiteConn.Open();                }                List
list = new List
{ }; list.Add("ID VARCHAR(5)");//汉字ID list.Add("Chinese VARCHAR(5)");//汉字 list.Add("English VARCHAR(10)");//拼音 CreateTabel(TableName, list); list.Clear(); } catch { // MessageBox.Show("创建日志数据库失败!"); } } public static bool ClearSystemLog() { try { if (_SQLiteConn.State != System.Data.ConnectionState.Open) { _SQLiteConn.Open(); } if (_SQLiteConn.State == System.Data.ConnectionState.Open) { _SQLiteCommand.CommandText = "delete from " + TableName + ";"; _SQLiteCommand.ExecuteNonQuery(); } _SQLiteConn.Close(); } catch (Exception ex) { // MessageBox.Show("清除日志失败:" + ex.Message); return false; } return true; } public static bool InsertData(string cn,string en,string id) { try { if (_SQLiteConn.State != System.Data.ConnectionState.Open) { _SQLiteConn.Open(); } if (_SQLiteConn.State == System.Data.ConnectionState.Open) { _SQLiteCommand.CommandText = "insert into " + TableName + " values('" + id + "','" + cn + "','" + en + "');"; _SQLiteCommand.ExecuteNonQuery(); } _SQLiteConn.Close(); } catch (Exception ex) { // MessageBox.Show("日志写入失败:" + ex.Message); return false; } return true; } public static List
GetData(string en) { List
list = new List
{ }; try { _SQLiteCommand.CommandText = "select * from " + TableName + " where English='"+en+"';"; using (SQLiteDataReader reader = _SQLiteCommand.ExecuteReader()) { string[] items = new string[] { }; while (reader.Read()) { items = new string[] { reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), }; list.Add(items); } } } catch (Exception ex) { MessageBox.Show(ex.Message + "=== GetDocInfo() ===" + ex.StackTrace); } return list; } public static List
GetZnData(string en) { en = en.ToLower(); ; List
list = new List
{ }; try { _SQLiteCommand.CommandText = "select * from " + TableName + " where English='" + en + "';"; // MessageBox.Show(_SQLiteCommand.CommandText); using (SQLiteDataReader reader = _SQLiteCommand.ExecuteReader()) { string[] items = new string[] { }; while (reader.Read()) { list.Add(reader["Chinese"].ToString()); } } } catch (Exception ex) { MessageBox.Show(ex.Message + "=== GetDocInfo() 2222 ===" + ex.StackTrace); } return list; } public static void CreateTabel(string tableName,List
columes ) { if (_SQLiteConn.State != System.Data.ConnectionState.Open) { _SQLiteConn.Open(); } if (_SQLiteConn.State == System.Data.ConnectionState.Open) { string sql = "SELECT COUNT(*) FROM sqlite_master where type='table' and name='" + tableName + "';"; _SQLiteCommand.CommandText = sql; if (Convert.ToInt32(_SQLiteCommand.ExecuteScalar()) == 0)//1表示存在,0表示不存 { sql = string.Empty; foreach (string str in columes) { sql += str + ","; } _SQLiteCommand.CommandText = string.Format( "CREATE TABLE {0} (" + sql.Substring(0, sql.Length - 1) + ")" , tableName); _SQLiteCommand.ExecuteNonQuery(); _SQLiteConn.Close(); } } else { MessageBox.Show("创建表失败,请打开数据库!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public static string PinConvert(string en) { string data = ""; string enLow = en.ToLower(); for (int i = 0; i < enLow.Length; i++) { if (enLow[i].ToString() == "ā") { } } return data; } #endregion }}

源码下载地址: 

更新 2017-2-13 ,还有个简单的方法 

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace TestForm{    public partial class Form1 : Form    {        [DllImport("user32.dll", EntryPoint = "keybd_event")]        public static extern void keybd_event(            byte bVk,                          //定义一个虚据拟键码。键码值必须在1~254之间。            byte bScan,                        //定义该键的硬件扫描码            int dwFlags,            int dwExtraInfo        );        private void button1_Click(object sender, EventArgs e)        {            // 81 表示Q,具体看虚拟键盘表示码            textBox1.Focus();            keybd_event(81, 0, 0, 0);                      //Q压下            keybd_event(81, 0, 0x02, 0);                   //Q弹起        }                public Form1()        {            InitializeComponent();        }         private void Form1_Load(object sender, EventArgs e)        {         }            }}
 
虚拟键盘码

你可能感兴趣的文章
在portal集成中如何判断BO超时,如超时跳转到portal登陆页上?
查看>>
SharePoint2010单点登录
查看>>
定制四则运算
查看>>
排版----缩略语(<title>)
查看>>
PHP实现压缩目录zip
查看>>
USB_CAN Tool数据分析说明
查看>>
Jdbc调用存储过程
查看>>
pl/sql development 查询的数据复制到excel
查看>>
java面向对象设计之实用程序类
查看>>
自定义指令的参数
查看>>
Stm32基础
查看>>
python实现进度条
查看>>
R语言中编写最小工作示例
查看>>
MySQL之事件学习整理
查看>>
mongodb开启安全认证后无法启动
查看>>
Android 一个应用启动另一个应用的说明
查看>>
阿里云CentOS7服务器利用LVM分区挂载磁盘全记录
查看>>
docker 常用命令
查看>>
计蒜客一维坐标的移动
查看>>
最全面的常用正则表达式大全
查看>>