千家信息网

C#如何打开软键盘

发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要介绍了C#如何打开软键盘,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。注:程序是在VS2005里面,注意一下。控件需要自己
千家信息网最后更新 2025年11月07日C#如何打开软键盘

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

  95. }

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

0