扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
1using System; 2using System.Runtime.InteropServices; 3 4namespace MyDemo 5{ 6 public static class clsIme 7 { 8 //声明一些API函数 9 [DllImport("imm32.dll")] 10 public static extern IntPtr ImmGetContext(IntPtr hwnd); 11 [DllImport("imm32.dll")] 12 public static extern bool ImmGetOpenStatus(IntPtr himc); 13 [DllImport("imm32.dll")] 14 public static extern bool ImmSetOpenStatus(IntPtr himc, bool b); 15 [DllImport("imm32.dll")] 16 public static extern bool ImmGetConversionStatus(IntPtr himc, ref int lpdw, ref int lpdw2); 17 [DllImport("imm32.dll")] 18 public static extern int ImmSimulateHotKey(IntPtr hwnd, int lngHotkey); 19 public const int IME_CMODE_FULLSHAPE = 0x8; 20 public const int IME_CHOTKEY_SHAPE_TOGGLE = 0x11; 21 //重载SetIme,传入Form 22 public static void SetIme(Form frm) 23 { 24 frm.Paint += new PaintEventHandler(frm_Paint); 25 ChangeAllControl(frm); 26 } 27 //重载SetIme,传入Control 28 public static void SetIme(Control ctl) 29 { 30 ChangeAllControl(ctl); 31 } 32 //重载SetIme,传入对象句柄 33 public static void SetIme(IntPtr Handel) 34 { 35 ChangeControlIme(Handel); 36 } 37 private static void ChangeAllControl(Control ctl) 38 { 39 //在控件的的Enter事件中触发来调整输入法状态 40 ctl.Enter += new EventHandler(ctl_Enter); 41 //遍历子控件,使每个控件都用上Enter的委托处理 42 foreach (Control ctlChild in ctl.Controls) 43 ChangeAllControl(ctlChild); 44 } 45 46 static void frm_Paint(object sender, PaintEventArgs e) 47 { 48 /**//*有人问为什么使用Pain事件,而不用Load事件或Activated事件,是基于下列考虑: 49 * 1、在您的Form中,有些控件可能是运行时动态添加的 50 * 2、在您的Form中,使用到了非.NET的OCX控件 51 * 3、Form调用子Form的时候,Activated事件根本不会触发 */ 52 ChangeControlIme(sender); 53 } 54 //控件的Enter处理程序 55 static void ctl_Enter(object sender, EventArgs e) 56 { 57 ChangeControlIme(sender); 58 } 59 private static void ChangeControlIme(object sender) 60 { 61 Control ctl = (Control)sender; 62 ChangeControlIme(ctl.Handle); 63 } 64 //下面这个函数才是真正检查输入法的全角半角状态 65 private static void ChangeControlIme(IntPtr h) 66 { 67 IntPtr HIme = ImmGetContext(h); 68 if (ImmGetOpenStatus(HIme)) //如果输入法处于打开状态 69 { 70 int iMode = 0; 71 int iSentence = 0; 72 bool bSuccess = ImmGetConversionStatus(HIme, ref iMode, ref iSentence); //检索输入法信息 73 if (bSuccess) 74 { 75 if ((iMode & IME_CMODE_FULLSHAPE) > 0) //如果是全角 76 ImmSimulateHotKey(h, IME_CHOTKEY_SHAPE_TOGGLE); //转换成半角 77 } 78 } 79 } 80 } 81} |
clsIme.SetIme(this); |
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Collections; 6using System.Drawing; 7using System.Text; 8using System.Windows.Forms; 9using System.Runtime.InteropServices; 10 11namespace MyDemo 12{ 13 public class ImeForm:System.Windows.Forms.Form 14 { 15 //声明一些API函数 16 [DllImport("imm32.dll")] 17 public static extern IntPtr ImmGetContext(IntPtr hwnd); 18 [DllImport("imm32.dll")] 19 public static extern bool ImmGetOpenStatus(IntPtr himc); 20 [DllImport("imm32.dll")] 21 public static extern bool ImmSetOpenStatus(IntPtr himc, bool b); 22 [DllImport("imm32.dll")] 23 public static extern bool ImmGetConversionStatus(IntPtr himc, ref int lpdw, ref int lpdw2); 24 [DllImport("imm32.dll")] 25 public static extern int ImmSimulateHotKey(IntPtr hwnd, int lngHotkey); 26 private const int IME_CMODE_FULLSHAPE = 0x8; 27 private const int IME_CHOTKEY_SHAPE_TOGGLE = 0x11; 28 //重载Form的OnActivated 29 protected override void OnActivated(EventArgs e) 30 { base.onActivated(e); 31 IntPtr HIme = ImmGetContext(this.Handle); 32 if (ImmGetOpenStatus(HIme)) //如果输入法处于打开状态 33 { 34 int iMode = 0; 35 int iSentence = 0; 36 bool bSuccess = ImmGetConversionStatus(HIme, ref iMode, ref iSentence); //检索输入法信息 37 if (bSuccess) 38 { 39 if ((iMode & IME_CMODE_FULLSHAPE) > 0) //如果是全角 40 ImmSimulateHotKey(this.Handle, IME_CHOTKEY_SHAPE_TOGGLE); //转换成半角 41 } 42 43 } 44 } 45 } 46} 47 |
public partial class Form1 :Form { ... } |
public partial class Form1 :ImeForm { ... } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者