.net framework 2.0 winform 半角全角问题全面解决
http://www.cnblogs.com/371448009/archive/2009/03/21/1418541.html
首先打补丁,然后在winform的imeMode = onHalf。如果不打补丁,会提示这个枚举不存在。
http://www.cnblogs.com/forrestsun/articles/810739.html
http://www.cnblogs.com/name-lh/archive/2006/04/13/374337.html
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 }
http://www.cnblogs.com/sunbingzibo/archive/2008/11/06/1328085.html
全角半角转化
#region 全角半角转换/// <summary>
/// 转全角的函数(SBC case)
/// </summary>
/// <param name="input">任意字符串</param>
/// <returns>全角字符串</returns>
///<remarks>
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
///</remarks>
public static string ToSBC(string input)
{
//半角转全角:
char[] c=input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i]==32)
{
c[i]=(char)12288;
continue;
}
if (c[i]<127)
c[i]=(char)(c[i]+65248);
}
return new string(c);
}
/// <summary> 转半角的函数(DBC case) </summary>
/// <param name="input">任意字符串</param>
/// <returns>半角字符串</returns>
///<remarks>
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
///</remarks>
public static string ToDBC(string input)
{
char[] c=input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i]==12288)
{
c[i]= (char)32;
continue;
}
if (c[i]>65280 && c[i]<65375)
c[i]=(char)(c[i]-65248);
}
return new string(c);
}
#endregion