这是一个微软的一个大BUG,目前无好的解决办法,我通过如下办法处理: 1、新建一个类:ImeForm,该类继承于Form, 2、在Load事件中,通过递归循环,查找所有的文本框,处理文本框的Enter事件,自动将输入法转换为半角,当然,如果其他输入控件也有类似问题,无非是在递归循环中多一个判断分支而已. 3、将程序中用到的Form的基类由System.Windows.Forms.Form改为ImeForm,如果以后微软解决了这个问题,改回去就可以了,ImeForm的全部代码如下: using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace Lqjt { /// <summary> /// 解决输入法全角的问题 /// </summary> public class ImeForm : System.Windows.Forms.Form { /// <summary> /// 做一个标记,避免重复设置Enter事件 /// </summary> private bool m_AttachProcessed = false; /// <summary> /// 在Load事件中遍历控件,如果是文本框,自动切换到半角状态 /// </summary> /// <param name="e"></param> protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (!m_AttachProcessed) { SetImeToHangul(this.Controls); m_AttachProcessed = true; } } /// <summary> /// 通过递归,遍历当前窗口的全部控件 /// </summary> /// <param name="p_Controls">容器</param> private void SetImeToHangul(System.Windows.Forms.Control.ControlCollection p_Controls) { foreach (System.Windows.Forms.Control ctl in p_Controls) { FlowLayoutPanel flpan = ctl as FlowLayoutPanel; if (flpan != null) { SetImeToHangul(flpan.Controls); continue; } GroupBox grp = ctl as GroupBox; if (grp != null) { SetImeToHangul(grp.Controls); continue; } Panel pan= ctl as Panel; if (pan != null) { SetImeToHangul(pan.Controls); continue; } TabControl tabcontrol = ctl as TabControl; if (tabcontrol != null) { SetImeToHangul(tabcontrol.Controls); continue; } TabPage tabpage = ctl as TabPage; if (tabpage != null) { SetImeToHangul(tabpage.Controls); continue; } TableLayoutPanel tlpan = ctl as TableLayoutPanel; if(tlpan!=null) { SetImeToHangul(tlpan.Controls); continue; } //文本框进入时,自动切换到半角,如果要控制其他可输入控件,参照下面的代码完成 TextBox txtbox = ctl as TextBox; if (txtbox != null) { txtbox.Enter += new EventHandler(ControlEnter_Enter); } } } private void ControlEnter_Enter(object sender, EventArgs e) { Control ctl = sender as Control; if (ctl == null) return; if (ctl.ImeMode != ImeMode.Hangul) ctl.ImeMode = ImeMode.Hangul; } }//class }
根据网上内容,我又重新修改了ImeForm,修改后的效果更好,解决了在NumbericUpDown以及用户通过Ctrl+Shift切换不能变半角的问题,新的如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;namespace Lqjt { public partial class ImeForm : Form { public ImeForm() { InitializeComponent(); } //声明一些API函数 [DllImport("imm32.dll")] public static extern IntPtr ImmGetContext(IntPtr hwnd); [DllImport("imm32.dll")] public static extern bool ImmGetOpenStatus(IntPtr himc); [DllImport("imm32.dll")] public static extern bool ImmSetOpenStatus(IntPtr himc, bool b); [DllImport("imm32.dll")] public static extern bool ImmGetConversionStatus(IntPtr himc, ref int lpdw, ref int lpdw2); [DllImport("imm32.dll")] public static extern int ImmSimulateHotKey(IntPtr hwnd, int lngHotkey); private const int IME_CMODE_FULLSHAPE = 0x8; private const int IME_CHOTKEY_SHAPE_TOGGLE = 0x11; protected override void OnActivated(EventArgs e) { base.OnActivated(e); IntPtr HIme = ImmGetContext(this.Handle); if (ImmGetOpenStatus(HIme)) //如果输入法处于打开状态 { int iMode = 0; int iSentence = 0; bool bSuccess = ImmGetConversionStatus(HIme, ref iMode, ref iSentence); //检索输入法信息 if (bSuccess) { if ((iMode & IME_CMODE_FULLSHAPE) > 0) //如果是全角 ImmSimulateHotKey(this.Handle, IME_CHOTKEY_SHAPE_TOGGLE); //转换成半角 } } } }//form }
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2
老大:你别误导人了,你的方法根本不行
1、新建一个类:ImeForm,该类继承于Form,
2、在Load事件中,通过递归循环,查找所有的文本框,处理文本框的Enter事件,自动将输入法转换为半角,当然,如果其他输入控件也有类似问题,无非是在递归循环中多一个判断分支而已.
3、将程序中用到的Form的基类由System.Windows.Forms.Form改为ImeForm,如果以后微软解决了这个问题,改回去就可以了,ImeForm的全部代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace Lqjt
{
/// <summary>
/// 解决输入法全角的问题
/// </summary>
public class ImeForm : System.Windows.Forms.Form
{
/// <summary>
/// 做一个标记,避免重复设置Enter事件
/// </summary>
private bool m_AttachProcessed = false;
/// <summary>
/// 在Load事件中遍历控件,如果是文本框,自动切换到半角状态
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!m_AttachProcessed)
{
SetImeToHangul(this.Controls);
m_AttachProcessed = true;
}
} /// <summary>
/// 通过递归,遍历当前窗口的全部控件
/// </summary>
/// <param name="p_Controls">容器</param>
private void SetImeToHangul(System.Windows.Forms.Control.ControlCollection p_Controls)
{
foreach (System.Windows.Forms.Control ctl in p_Controls)
{
FlowLayoutPanel flpan = ctl as FlowLayoutPanel;
if (flpan != null)
{
SetImeToHangul(flpan.Controls);
continue;
}
GroupBox grp = ctl as GroupBox;
if (grp != null)
{
SetImeToHangul(grp.Controls);
continue;
} Panel pan= ctl as Panel;
if (pan != null)
{
SetImeToHangul(pan.Controls);
continue;
}
TabControl tabcontrol = ctl as TabControl;
if (tabcontrol != null)
{
SetImeToHangul(tabcontrol.Controls);
continue;
}
TabPage tabpage = ctl as TabPage;
if (tabpage != null)
{
SetImeToHangul(tabpage.Controls);
continue;
}
TableLayoutPanel tlpan = ctl as TableLayoutPanel;
if(tlpan!=null)
{
SetImeToHangul(tlpan.Controls);
continue;
}
//文本框进入时,自动切换到半角,如果要控制其他可输入控件,参照下面的代码完成
TextBox txtbox = ctl as TextBox;
if (txtbox != null)
{
txtbox.Enter += new EventHandler(ControlEnter_Enter);
}
}
}
private void ControlEnter_Enter(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (ctl == null)
return;
if (ctl.ImeMode != ImeMode.Hangul)
ctl.ImeMode = ImeMode.Hangul;
}
}//class
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace Lqjt
{
public partial class ImeForm : Form
{
public ImeForm()
{
InitializeComponent();
}
//声明一些API函数
[DllImport("imm32.dll")]
public static extern IntPtr ImmGetContext(IntPtr hwnd);
[DllImport("imm32.dll")]
public static extern bool ImmGetOpenStatus(IntPtr himc);
[DllImport("imm32.dll")]
public static extern bool ImmSetOpenStatus(IntPtr himc, bool b);
[DllImport("imm32.dll")]
public static extern bool ImmGetConversionStatus(IntPtr himc, ref int lpdw, ref int lpdw2);
[DllImport("imm32.dll")]
public static extern int ImmSimulateHotKey(IntPtr hwnd, int lngHotkey);
private const int IME_CMODE_FULLSHAPE = 0x8;
private const int IME_CHOTKEY_SHAPE_TOGGLE = 0x11;
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
IntPtr HIme = ImmGetContext(this.Handle);
if (ImmGetOpenStatus(HIme)) //如果输入法处于打开状态
{
int iMode = 0;
int iSentence = 0;
bool bSuccess = ImmGetConversionStatus(HIme, ref iMode, ref iSentence); //检索输入法信息
if (bSuccess)
{
if ((iMode & IME_CMODE_FULLSHAPE) > 0) //如果是全角
ImmSimulateHotKey(this.Handle, IME_CHOTKEY_SHAPE_TOGGLE); //转换成半角
} }
}
}//form
}