VS2005中文输入法自动转换为全角的两种解决方法

作者:南疯

      最近在用VS2005做项目的时候,一直忍受着VS2005输入法自动切换到全角的Bug的作怪,一边等待着微软给我们一个解决的方案。但是,我的项目都要作为产品打包出去了,微软还是闷头不对这个Bug出一个解决方法。怎么办?我可以忍受这个输入法来回切换之苦,可用户体验可不会饶过我们的。弄不好,来个集体罢用,让我们都到微软喝西北风去啊!
      总不能就这么交出产品出去吧,只有自己动手了。下面我用两种方法来实现如何避免输入法的这个Bug。
方法一:
Form的Pain和遍历Control的Enter方法。
      首先,我们为了使您原有的代码更简洁,我们把所要做的步骤封装到一个单独的类中,类代码如下:

 1 None.gif using  System;
 2 None.gif using  System.Runtime.InteropServices;
 3 None.gif
 4 None.gif namespace  MyDemo
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif
 6InBlock.gif    public static class clsIme
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        //声明一些API函数
 9InBlock.gif        [DllImport("imm32.dll")]
10InBlock.gif        public static extern IntPtr ImmGetContext(IntPtr hwnd);
11InBlock.gif        [DllImport("imm32.dll")]
12InBlock.gif        public static extern bool ImmGetOpenStatus(IntPtr himc);
13InBlock.gif        [DllImport("imm32.dll")]
14InBlock.gif        public static extern bool ImmSetOpenStatus(IntPtr himc, bool b);
15InBlock.gif        [DllImport("imm32.dll")]
16InBlock.gif        public static extern bool ImmGetConversionStatus(IntPtr himc, ref int lpdw, ref int lpdw2);
17InBlock.gif        [DllImport("imm32.dll")]
18InBlock.gif        public static extern int ImmSimulateHotKey(IntPtr hwnd, int lngHotkey);
19InBlock.gif        public const int IME_CMODE_FULLSHAPE = 0x8;
20InBlock.gif        public const int IME_CHOTKEY_SHAPE_TOGGLE = 0x11;
21InBlock.gif        //重载SetIme,传入Form
22InBlock.gif        public static void SetIme(Form frm)
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            frm.Paint += new PaintEventHandler(frm_Paint);
25InBlock.gif            ChangeAllControl(frm);
26ExpandedSubBlockEnd.gif        }

27InBlock.gif        //重载SetIme,传入Control
28InBlock.gif        public static void SetIme(Control ctl)
29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
30InBlock.gif            ChangeAllControl(ctl);
31ExpandedSubBlockEnd.gif        }

32InBlock.gif        //重载SetIme,传入对象句柄
33InBlock.gif        public static void SetIme(IntPtr Handel)
34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
35InBlock.gif            ChangeControlIme(Handel);
36ExpandedSubBlockEnd.gif        }

37InBlock.gif        private static void ChangeAllControl(Control ctl)
38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
39InBlock.gif            //在控件的的Enter事件中触发来调整输入法状态
40InBlock.gif            ctl.Enter += new EventHandler(ctl_Enter);
41InBlock.gif            //遍历子控件,使每个控件都用上Enter的委托处理
42InBlock.gif            foreach (Control ctlChild in ctl.Controls)
43InBlock.gif                ChangeAllControl(ctlChild);
44ExpandedSubBlockEnd.gif        }

45InBlock.gif
46InBlock.gif        static void frm_Paint(object sender, PaintEventArgs e)
47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
48ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//*有人问为什么使用Pain事件,而不用Load事件或Activated事件,是基于下列考虑:
49InBlock.gif             * 1、在您的Form中,有些控件可能是运行时动态添加的
50InBlock.gif             * 2、在您的Form中,使用到了非.NET的OCX控件
51ExpandedSubBlockEnd.gif             * 3、Form调用子Form的时候,Activated事件根本不会触发 */

52InBlock.gif            ChangeControlIme(sender);
53ExpandedSubBlockEnd.gif        }

54InBlock.gif        //控件的Enter处理程序
55InBlock.gif        static void ctl_Enter(object sender, EventArgs e)
56ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
57InBlock.gif            ChangeControlIme(sender);
58ExpandedSubBlockEnd.gif        }

59InBlock.gif        private static void ChangeControlIme(object sender)
60ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
61InBlock.gif            Control ctl = (Control)sender;
62InBlock.gif            ChangeControlIme(ctl.Handle);
63ExpandedSubBlockEnd.gif        }

64InBlock.gif        //下面这个函数才是真正检查输入法的全角半角状态
65InBlock.gif        private static void ChangeControlIme(IntPtr h)
66ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
67InBlock.gif            IntPtr HIme = ImmGetContext(h);            
68InBlock.gif            if (ImmGetOpenStatus(HIme))  //如果输入法处于打开状态
69ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
70InBlock.gif                int iMode = 0;
71InBlock.gif                int iSentence = 0;
72InBlock.gif                bool bSuccess = ImmGetConversionStatus(HIme, ref iMode, ref iSentence);  //检索输入法信息
73InBlock.gif                if (bSuccess)
74ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
75InBlock.gif                    if ((iMode & IME_CMODE_FULLSHAPE) > 0)   //如果是全角
76InBlock.gif                        ImmSimulateHotKey(h, IME_CHOTKEY_SHAPE_TOGGLE);  //转换成半角
77ExpandedSubBlockEnd.gif                }

78ExpandedSubBlockEnd.gif            }

79ExpandedSubBlockEnd.gif        }

80ExpandedSubBlockEnd.gif    }

81ExpandedBlockEnd.gif}


      有人问为什么使用Pain事件,而不用Load事件或Activated事件,我是基于下列考虑:

      1、在您的Form中,有些控件可能是运行时动态添加的
      2、在您的Form中,使用到了非.NET的OCX控件
      3、Form调用子Form的时候,Activated事件根本不会触发

使用这个类的方法为:
      在您的界面中,在Load的时候,在里面加上这样一句话:
      clsIme.SetIme(this);

方法二:
使用继承的方法。
      首先,建立一个独立的类如下:
      

 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.ComponentModel;
 4 None.gif using  System.Data;
 5 None.gif using  System.Collections;
 6 None.gif using  System.Drawing;
 7 None.gif using  System.Text;
 8 None.gif using  System.Windows.Forms;
 9 None.gif using  System.Runtime.InteropServices;
10 None.gif
11 None.gif namespace  MyDemo
12 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
13InBlock.gif    public class ImeForm:System.Windows.Forms.Form 
14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
15InBlock.gif        //声明一些API函数
16InBlock.gif        [DllImport("imm32.dll")]
17InBlock.gif        public static extern IntPtr ImmGetContext(IntPtr hwnd);
18InBlock.gif        [DllImport("imm32.dll")]
19InBlock.gif        public static extern bool ImmGetOpenStatus(IntPtr himc);
20InBlock.gif        [DllImport("imm32.dll")]
21InBlock.gif        public static extern bool ImmSetOpenStatus(IntPtr himc, bool b);
22InBlock.gif        [DllImport("imm32.dll")]
23InBlock.gif        public static extern bool ImmGetConversionStatus(IntPtr himc, ref int lpdw, ref int lpdw2);
24InBlock.gif        [DllImport("imm32.dll")]
25InBlock.gif        public static extern int ImmSimulateHotKey(IntPtr hwnd, int lngHotkey);
26InBlock.gif        private  const int IME_CMODE_FULLSHAPE = 0x8;
27InBlock.gif        private  const int IME_CHOTKEY_SHAPE_TOGGLE = 0x11;
28InBlock.gif        //重载Form的OnActivated
29InBlock.gif        protected override void OnActivated(EventArgs e)
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{              
                     base.onActivated(e);           
31InBlock.gif            IntPtr HIme = ImmGetContext(this.Handle);
32InBlock.gif            if (ImmGetOpenStatus(HIme))  //如果输入法处于打开状态
33ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
34InBlock.gif                int iMode = 0;
35InBlock.gif                int iSentence = 0;
36InBlock.gif                bool bSuccess = ImmGetConversionStatus(HIme, ref iMode, ref iSentence);  //检索输入法信息
37InBlock.gif                if (bSuccess)
38ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
39InBlock.gif                    if ((iMode & IME_CMODE_FULLSHAPE) > 0)   //如果是全角
40InBlock.gif                        ImmSimulateHotKey(this.Handle, IME_CHOTKEY_SHAPE_TOGGLE);  //转换成半角
41ExpandedSubBlockEnd.gif                }

42InBlock.gif
43ExpandedSubBlockEnd.gif            }

44ExpandedSubBlockEnd.gif        }
        
45ExpandedSubBlockEnd.gif    }

46ExpandedBlockEnd.gif}

47 None.gif


      使用这个类的方法为:
      修改所有的Form的继承关系,比如,你有这样的一个Form类:
      public partial class Form1 :Form 
      {
      ...
      }
      那么,把它改成:
      public partial class Form1 :ImeForm
      {
      ...
      }
      相信,这样的修改会很快,全项目查找替换一下即可。
      记住,如果你的Form是多重继承下来的,例如:FormC派生于FormB,而FormB又派生于FormA,那么,仅仅需要FormA从imeForm派生即可。
方法二的使用优势是明显的,把Ime的事件从Form最上一层就截取了,避免了在您的Form中控件的多样性所带来的困扰。
      还有,网上有一些说的调整ImeMode和使用ImeModeChanged方法来解决这个问题,建议你暂时(只是暂时)不要使用,因为修改ImeMode根本不能解决窗口切换时输入法自动变全角的问题,而且ImeModeChanged是在ImeMode改变的时候才触发,在用户手工操作输入法状态改变时(比如按Ctrl+Shift)是不会触发的。




PS:最近微软出了补丁。详情请见 这里

转载于:https://www.cnblogs.com/name-lh/archive/2006/04/13/374337.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值