C#打开软键盘的方法

开发 后端
这里介绍C#打开软键盘的方法,程序是在VS2005里面,注意一下。控件需要自己添加了。一个ComboBox,一个Textbox,一个Button,直接拖进去没有更改name。

以前曾经见别人CSDN上面问过如何用C#打开软键盘,我也关注过,可没有结果。在网上搜了一下也没有能找出满意的答案。今天就拿这个开开刀吧,反正大项目也没有,只是一个小功能。结果没曾想,弄出来了。也有好久没有更新我的这一亩三分田了,就放上来了。

献给那些为中国共享软件和提高中国程序水平作努力的人们。

注:程序是在VS2005里面,注意一下。控件需要自己添加了。一个ComboBox,一个Textbox,一个Button,直接拖进去没有更改name

用C#打开软键盘程序开始

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Runtime.InteropServices;  
  9.  
  10. namespace InputLanguageSwitch  
  11. {  
  12. /// <summary> 
  13. /// 获取系统所有的输入法,并可以程序改变当前输入法;打开关闭软键盘。  
  14. /// 程序:房客  
  15. /// QQ:83849123  
  16. /// Blog:http://www.cnblogs.com/sxlfybb  
  17. /// </summary> 
  18. public partial class Form1 : Form  
  19. {  
  20. public Form1()  
  21. {  
  22. InitializeComponent();  
  23. this.InputLanguageChanged += new InputLanguageChangedEventHandler
    (Form1_InputLanguageChanged);  
  24. }  
  25.  
  26. void Form1_InputLanguageChanged(object sender, InputLanguageChangedEventArgs e)  
  27. {  
  28. //throw new Exception("The method or operation is not implemented.");  
  29. for( int i=0;i<comboBox1.Items.Count;i++)  
  30. {  
  31. if (InputLanguage.CurrentInputLanguage.LayoutName == comboBox1.Items[i].ToString())  
  32. {  
  33. comboBox1.SelectedIndex = i;  
  34. }  
  35.  
  36. }  
  37. }  
  38.  
  39. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
  40. {  
  41. string strTemp = comboBox1.Items[comboBox1.SelectedIndex].ToString();  
  42. InputLanguageCollection collects = InputLanguage.InstalledInputLanguages;  
  43. for (int i = 0; i < collects.Count; i++)  
  44. {  
  45. if (collects[i].LayoutName == strTemp)  
  46. InputLanguage.CurrentInputLanguage = collects[i];  
  47. }  
  48.  
  49. }  
  50.  
  51. private void Form1_Load(object sender, EventArgs e)  
  52. {  
  53. InputLanguageCollection collects = InputLanguage.InstalledInputLanguages;  
  54. for (int i = 0; i < collects.Count; i++)  
  55. {  
  56. comboBox1.Items.Add(collects[i].LayoutName);  
  57. }  
  58. comboBox1.SelectedIndex = collects.IndexOf(InputLanguage.CurrentInputLanguage);  
  59. }  
  60.  
  61. private void button1_Click(object sender, EventArgs e)  
  62. {  
  63. this.textBox1.Focus();  
  64. IntPtr hwndInput = ImmGetContext(this.Handle);  
  65. IntPtr dw1 = IntPtr.Zero;  
  66. IntPtr dw2 = IntPtr.Zero;  
  67. bool isSuccess = ImmGetConversionStatus(hwndInput, ref dw1, ref dw2);  
  68. if (isSuccess)  
  69. {  
  70. int intTemp = dw1.ToInt32() & IME_CMODE_SOFTKBD;  
  71. if (intTemp > 0)  
  72. dw1 = (IntPtr)(dw1.ToInt32() ^ IME_CMODE_SOFTKBD);  
  73. else  
  74. dw1 = (IntPtr)(dw1.ToInt32() IME_CMODE_SOFTKBD);  
  75. }  
  76. isSuccess = ImmSetConversionStatus(hwndInput, dw1, dw2);  
  77. ImmReleaseContext(this.Handle, hwndInput);  
  78. }  
  79.  
  80. public const int IME_CMODE_SOFTKBD = 0x80;  
  81.  
  82. [DllImport("imm32.dll", EntryPoint = "ImmGetContext")]  
  83. public static extern IntPtr ImmGetContext(  
  84. IntPtr hwnd  
  85. );  
  86. [DllImport("imm32.dll", EntryPoint = "ImmGetConversionStatus")]  
  87. public static extern bool ImmGetConversionStatus(  
  88. IntPtr himc,  
  89. ref IntPtr lpdw,  
  90. ref IntPtr lpdw2  
  91. );  
  92. [DllImport("imm32.dll", EntryPoint = "ImmSetConversionStatus")]  
  93. public static extern bool ImmSetConversionStatus(  
  94. IntPtr himc,  
  95. IntPtr dw1,  
  96. IntPtr dw2  
  97. );  
  98.  
  99. [DllImport("imm32.dll", EntryPoint = "ImmReleaseContext")]  
  100. public static extern int ImmReleaseContext(  
  101. IntPtr hwnd,  
  102. IntPtr himc  
  103. );  
  104.  
  105. }  


用C#打开软键盘代码结束。

另外,也可以直接执行OSK而直接打开虚拟键盘,不过这个和软键盘是不一样的。
现在反钓鱼的软件越来越多了,祝大家共同进步。

【编辑推荐】

  1. 如何用C#和ADO.NET访问
  2. 浅析C# Switch语句
  3. C#验证输入方法详解
  4. 简单介绍C# 匿名方法
  5. C# FileSystemWatcher对象
责任编辑:佚名 来源: IT168
相关推荐

2021-08-07 15:31:45

Windows 10Windows微软

2024-03-06 09:16:57

PAD设备kikaInput鸿蒙

2017-12-05 15:26:19

2020-03-24 09:34:00

移动端H5软键盘

2017-12-05 13:12:35

Android软键盘参数

2017-07-03 17:20:55

Android软键盘控制开发问题

2021-07-13 09:49:08

鸿蒙HarmonyOS应用

2019-04-25 10:20:22

H5软键盘前端

2013-06-27 17:26:01

AndroidEditText

2013-04-01 17:05:28

2009-09-01 14:18:09

C#打开Excel文档

2019-08-05 08:02:31

AI 数据人工智能

2009-08-19 15:54:33

处理C#消息

2009-08-26 09:50:08

C# GreetPeo

2009-08-26 17:16:22

C# CheckSta

2009-08-17 17:40:53

C# GetAllCu

2009-08-10 17:00:14

C#分部方法

2009-08-03 12:57:27

C#调用DLL

2009-09-01 13:13:28

C#打开Word文档

2009-08-27 17:39:51

C#多个方法的关联
点赞
收藏

51CTO技术栈公众号