C# Winform制作虚拟键盘,支持中文

原文链接:https://blog.csdn.net/taoerit/article/details/53037637,支持原创,转载学习

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

       实现思路:

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

            

 

         2 构建布局,如效果图

 

 

代码:

  数据库代码文件  SqlHandler.cs

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data.SQLite;  
  6. using System.Configuration;  
  7. using System.IO;  
  8. using System.Reflection;  
  9. using System.Windows.Forms;  
  10.   
  11.   
  12. namespace TestKeyBord  
  13. {  
  14.    
  15.     public class SqlHandler  
  16.     {  
  17.         public  static void InitSQLite(string db,string table)  
  18.         {  
  19.             try  
  20.             {  
  21.                 DbName = db;  
  22.                 TableName = table;  
  23.   
  24.                 if (CreateDataBase())  
  25.                 {  
  26.                     _SQLiteCommand = _SQLiteConn.CreateCommand();  
  27.                     _SQLiteCommand.Connection = _SQLiteConn;  
  28.                     DesignerTable();  
  29.                 }  
  30.             }  
  31.             catch  
  32.             {  
  33.                 
  34.             }  
  35.         }  
  36.   
  37.         public static System.Data.ConnectionState SqliteState  
  38.         {  
  39.             get { return _SQLiteConn.State; }  
  40.         }  
  41.  
  42.  
  43.         #region 数据成员定义  
  44.           
  45.         public static string DbName = "MedicalSystemLog";  
  46.         public static string TableName = "MedicalLog";  
  47.         public static string _SQLiteConnString = string.Empty;  
  48.   
  49.         public static SQLiteConnection _SQLiteConn = new SQLiteConnection();  
  50.          
  51.         public static SQLiteCommand _SQLiteCommand = new SQLiteCommand();  
  52.                 
  53.        
  54.         #endregion  
  55.  
  56.         #region 创建数据库文件  
  57.   
  58.         public static bool CreateDataBase()  
  59.         {  
  60.             try  
  61.             {  
  62.                 _SQLiteConnString = "Data Source=" + DbName + ".db";  
  63.                 _SQLiteConn = new SQLiteConnection(_SQLiteConnString);  
  64.                 _SQLiteConn.Open();  
  65.                 _SQLiteCommand = _SQLiteConn.CreateCommand();  
  66.                 _SQLiteCommand.Connection = _SQLiteConn;  
  67.   
  68.                 if (File.Exists(DbName + ".db"))  
  69.                 {  
  70.                     return true;  
  71.                 }  
  72.             }  
  73.             catch  
  74.             {  
  75.                // MessageBox.Show("日志系统加载失败!");  
  76.             }  
  77.             return false;  
  78.         }  
  79.  
  80.         #endregion  
  81.   
  82.         /// <summary>  
  83.         /// 矩阵是否连接  
  84.         /// </summary>  
  85.         public static bool MatrixIsConnected = false;  
  86.  
  87.  
  88.         #region 创建表  
  89.    
  90.   
  91.         public static void DesignerTable()  
  92.         {  
  93.             try  
  94.             {  
  95.                 if (_SQLiteConn.State != System.Data.ConnectionState.Open)  
  96.                 {  
  97.                     _SQLiteConn.Open();  
  98.                 }  
  99.   
  100.                 List<string> list = new List<string> { };  
  101.                 list.Add("ID VARCHAR(5)");//汉字ID  
  102.                 list.Add("Chinese VARCHAR(5)");//汉字  
  103.                 list.Add("English VARCHAR(10)");//拼音  
  104.                 CreateTabel(TableName, list);  
  105.                 list.Clear();  
  106.             }  
  107.             catch  
  108.             {  
  109.                // MessageBox.Show("创建日志数据库失败!");  
  110.             }  
  111.         }  
  112.   
  113.   
  114.   
  115.         public static bool ClearSystemLog()  
  116.         {  
  117.             try  
  118.             {  
  119.                   
  120.   
  121.                 if (_SQLiteConn.State != System.Data.ConnectionState.Open)  
  122.                 {  
  123.                     _SQLiteConn.Open();  
  124.                 }  
  125.   
  126.                 if (_SQLiteConn.State == System.Data.ConnectionState.Open)  
  127.                 {  
  128.   
  129.                     _SQLiteCommand.CommandText = "delete from " + TableName + ";";  
  130.                     _SQLiteCommand.ExecuteNonQuery();  
  131.   
  132.                 }  
  133.   
  134.                 _SQLiteConn.Close();  
  135.             }  
  136.               
  137.              catch (Exception ex)  
  138.              {  
  139.                    // MessageBox.Show("清除日志失败:" + ex.Message);  
  140.                     return false;  
  141.               }  
  142.             return true;  
  143.         }  
  144.   
  145.   
  146.        
  147.         public static bool InsertData(string cn,string en,string id)  
  148.         {  
  149.             try  
  150.             {  
  151.                  
  152.   
  153.                 if (_SQLiteConn.State != System.Data.ConnectionState.Open)  
  154.                 {  
  155.                     _SQLiteConn.Open();  
  156.                 }  
  157.   
  158.                 if (_SQLiteConn.State == System.Data.ConnectionState.Open)  
  159.                 {  
  160.   
  161.                     _SQLiteCommand.CommandText = "insert into " + TableName + " values('" +  
  162.                        id + "','" + cn + "','" + en +  "');";  
  163.                     _SQLiteCommand.ExecuteNonQuery();  
  164.   
  165.                 }  
  166.   
  167.                 _SQLiteConn.Close();  
  168.             }  
  169.             catch (Exception ex)  
  170.             {  
  171.                // MessageBox.Show("日志写入失败:" + ex.Message);  
  172.                 return false;  
  173.             }  
  174.             return true;  
  175.         }  
  176.   
  177.         
  178.   
  179.   
  180.         public static List<string[]> GetData(string en)  
  181.         {  
  182.             List<string[]> list = new List<string[]> { };  
  183.   
  184.             try  
  185.             {  
  186.   
  187.                 _SQLiteCommand.CommandText = "select * from " + TableName + " where English='"+en+"';";  
  188.   
  189.                 using (SQLiteDataReader reader = _SQLiteCommand.ExecuteReader())  
  190.                 {  
  191.   
  192.                     string[] items = new string[] { };  
  193.   
  194.                     while (reader.Read())  
  195.                     {  
  196.                         items = new string[]  
  197.                         {   
  198.                             reader[0].ToString(),  
  199.                             reader[1].ToString(),  
  200.                             reader[2].ToString(),  
  201.                         };  
  202.                         list.Add(items);  
  203.                     }  
  204.   
  205.                 }  
  206.             }  
  207.   
  208.             catch (Exception ex)  
  209.             {  
  210.                 MessageBox.Show(ex.Message + "=== GetDocInfo() ===" + ex.StackTrace);  
  211.             }  
  212.             return list;  
  213.         }  
  214.   
  215.   
  216.         public static List<string> GetZnData(string en)  
  217.         {  
  218.   
  219.             en = en.ToLower(); ;  
  220.   
  221.             List<string> list = new List<string> { };  
  222.   
  223.             try  
  224.             {  
  225.   
  226.                 _SQLiteCommand.CommandText = "select * from " + TableName + " where English='" + en + "';";  
  227.              //  MessageBox.Show(_SQLiteCommand.CommandText);  
  228.                 using (SQLiteDataReader reader = _SQLiteCommand.ExecuteReader())  
  229.                 {  
  230.   
  231.                     string[] items = new string[] { };  
  232.   
  233.                     while (reader.Read())  
  234.                     {  
  235.                           
  236.                         list.Add(reader["Chinese"].ToString());  
  237.                     }  
  238.   
  239.                 }  
  240.             }  
  241.   
  242.             catch (Exception ex)  
  243.             {  
  244.                 MessageBox.Show(ex.Message + "=== GetDocInfo() 2222 ===" + ex.StackTrace);  
  245.             }  
  246.             return list;  
  247.         }  
  248.   
  249.         public static void CreateTabel(string tableName,List<string> columes )  
  250.         {  
  251.             if (_SQLiteConn.State != System.Data.ConnectionState.Open)  
  252.             {  
  253.                 _SQLiteConn.Open();  
  254.             }  
  255.   
  256.             if (_SQLiteConn.State == System.Data.ConnectionState.Open)  
  257.             {  
  258.                   
  259.                 string sql = "SELECT COUNT(*) FROM sqlite_master where type='table' and name='" + tableName + "';";  
  260.                  
  261.                 _SQLiteCommand.CommandText = sql;  
  262.   
  263.                 if (Convert.ToInt32(_SQLiteCommand.ExecuteScalar()) == 0)//1表示存在,0表示不存  
  264.                 {  
  265.                     sql = string.Empty;  
  266.   
  267.                     foreach (string str in columes)  
  268.                     {  
  269.                         sql += str + ",";  
  270.                     }  
  271.   
  272.                     _SQLiteCommand.CommandText = string.Format(  
  273.                         "CREATE TABLE {0} (" + sql.Substring(0, sql.Length - 1) + ")"  
  274.                         , tableName);  
  275.   
  276.                     _SQLiteCommand.ExecuteNonQuery();  
  277.                     _SQLiteConn.Close();  
  278.                 }  
  279.   
  280.             }  
  281.             else  
  282.             {  
  283.                 MessageBox.Show("创建表失败,请打开数据库!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  284.             }  
  285.         }  
  286.   
  287.   
  288.   
  289.         public static string PinConvert(string en)   
  290.         {  
  291.             string data = "";  
  292.             string enLow = en.ToLower();  
  293.             for (int i = 0; i < enLow.Length; i++)  
  294.             {  
  295.                 if (enLow[i].ToString() == "ā")  
  296.                 {  
  297.   
  298.                 }  
  299.             }  
  300.             return data;  
  301.         }  
  302.  
  303.         #endregion  
  304.     }  
  305. }  

 

 

源码下载地址: http://download.csdn.net/detail/taoerit/9686889

 

 

 

 

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

 

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Runtime.InteropServices;  
  10.   
  11. namespace TestForm  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         [DllImport("user32.dll", EntryPoint = "keybd_event")]  
  16.         public static extern void keybd_event(  
  17.             byte bVk,                          //定义一个虚据拟键码。键码值必须在1~254之间。  
  18.             byte bScan,                        //定义该键的硬件扫描码  
  19.             int dwFlags,  
  20.             int dwExtraInfo  
  21.         );  
  22.   
  23.         private void button1_Click(object sender, EventArgs e)  
  24.         {  
  25.             // 81 表示Q,具体看虚拟键盘表示码  
  26.             textBox1.Focus();  
  27.             keybd_event(81, 0, 0, 0);                      //Q压下  
  28.             keybd_event(81, 0, 0x02, 0);                   //Q弹起  
  29.         }  
  30.           
  31.         public Form1()  
  32.         {  
  33.             InitializeComponent();  
  34.         }  
  35.   
  36.    
  37.         private void Form1_Load(object sender, EventArgs e)  
  38.         {   
  39.         }  
  40.   
  41.           
  42.     }  
  43. }  
 
[csharp] view plain copy
 
    1. <span style="color:#ff0000;">虚拟键盘码</span>  

 

 

posted @ 2018-06-15 15:37  傻蛋儿  阅读(1286)  评论(0)    收藏  举报