C#如何打开软键盘

发布时间:2021-12-01 16:23:37 作者:小新
阅读:1326
开发者专用服务器限时活动,0元免费领! 查看>>

这篇文章主要介绍了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. namespace InputLanguageSwitch  

  10. {  

  11. /// <summary> 

  12. /// 获取系统所有的输入法,并可以程序改变当前输入法;打开关闭软键盘。  

  13. /// 程序:房客  

  14. /// QQ:83849123  

  15. /// Blog:http://www.cnblogs.com/sxlfybb  

  16. /// </summary> 

  17. public partial class Form1 : Form  

  18. {  

  19. public Form1()  

  20. {  

  21. InitializeComponent();  

  22. this.InputLanguageChanged += new InputLanguageChangedEventHandler
    (Form1_InputLanguageChanged);  

  23. }  

  24. void Form1_InputLanguageChanged(object sender, InputLanguageChangedEventArgs e)  

  25. {  

  26. //throw new Exception("The method or operation is not implemented.");  

  27. for( int i=0;i<comboBox1.Items.Count;i++)  

  28. {  

  29. if (InputLanguage.CurrentInputLanguage.LayoutName == comboBox1.Items[i].ToString())  

  30. {  

  31. comboBox1.SelectedIndex = i;  

  32. }  

  33. }  

  34. }  

  35. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  

  36. {  

  37. string strTemp = comboBox1.Items[comboBox1.SelectedIndex].ToString();  

  38. InputLanguageCollection collects = InputLanguage.InstalledInputLanguages;  

  39. for (int i = 0; i < collects.Count; i++)  

  40. {  

  41. if (collects[i].LayoutName == strTemp)  

  42. InputLanguage.CurrentInputLanguage = collects[i];  

  43. }  

  44. }  

  45. private void Form1_Load(object sender, EventArgs e)  

  46. {  

  47. InputLanguageCollection collects = InputLanguage.InstalledInputLanguages;  

  48. for (int i = 0; i < collects.Count; i++)  

  49. {  

  50. comboBox1.Items.Add(collects[i].LayoutName);  

  51. }  

  52. comboBox1.SelectedIndex = collects.IndexOf(InputLanguage.CurrentInputLanguage);  

  53. }  

  54. private void button1_Click(object sender, EventArgs e)  

  55. {  

  56. this.textBox1.Focus();  

  57. IntPtr hwndInput = ImmGetContext(this.Handle);  

  58. IntPtr dw1 = IntPtr.Zero;  

  59. IntPtr dw2 = IntPtr.Zero;  

  60. bool isSuccess = ImmGetConversionStatus(hwndInput, ref dw1, ref dw2);  

  61. if (isSuccess)  

  62. {  

  63. int intTemp = dw1.ToInt32() & IME_CMODE_SOFTKBD;  

  64. if (intTemp > 0)  

  65. dw1 = (IntPtr)(dw1.ToInt32() ^ IME_CMODE_SOFTKBD);  

  66. else  

  67. dw1 = (IntPtr)(dw1.ToInt32() IME_CMODE_SOFTKBD);  

  68. }  

  69. isSuccess = ImmSetConversionStatus(hwndInput, dw1, dw2);  

  70. ImmReleaseContext(this.Handle, hwndInput);  

  71. }  

  72. public const int IME_CMODE_SOFTKBD = 0x80;  

  73. [DllImport("imm32.dll", EntryPoint = "ImmGetContext")]  

  74. public static extern IntPtr ImmGetContext(  

  75. IntPtr hwnd  

  76. );  

  77. [DllImport("imm32.dll", EntryPoint = "ImmGetConversionStatus")]  

  78. public static extern bool ImmGetConversionStatus(  

  79. IntPtr himc,  

  80. ref IntPtr lpdw,  

  81. ref IntPtr lpdw2  

  82. );  

  83. [DllImport("imm32.dll", EntryPoint = "ImmSetConversionStatus")]  

  84. public static extern bool ImmSetConversionStatus(  

  85. IntPtr himc,  

  86. IntPtr dw1,  

  87. IntPtr dw2  

  88. );  

  89. [DllImport("imm32.dll", EntryPoint = "ImmReleaseContext")]  

  90. public static extern int ImmReleaseContext(  

  91. IntPtr hwnd,  

  92. IntPtr himc  

  93. );  

  94. }  

感谢你能够认真阅读完这篇文章,希望小编分享的“C#如何打开软键盘”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:
  1. C#防止窗体多次打开
  2. c#打开py文件的方法

开发者交流群:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:轻量级Swing组件怎么用

下一篇:C#中Convert类解决方法的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
开发者交流群×