using System.Runtime.InteropServices;
[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 ImmGetConversionStatus(IntPtr hIMC,
ref int conversion, ref int sentence);
[DllImport("imm32.dll")]
public static extern bool ImmReleaseContext(IntPtr hWnd, IntPtr hIMC);
[DllImport("imm32.dll")]
public static extern bool ImmSetConversionStatus(IntPtr hIMC,
int conversion, int sentence);
const int IME_CMODE_FULLSHAPE= 0x0008;
public static void SetHalfShape(IntPtr AHandle)
{
IntPtr vIme = ImmGetContext(AHandle);
if (ImmGetOpenStatus(vIme)) // 输入法是打开的
{
int vMode = 0, vSentence = 0;
if (ImmGetConversionStatus(vIme, ref vMode, ref vSentence)) // 获取输入法状态
{
if ((vMode & IME_CMODE_FULLSHAPE) > 0) // 是全角
{
vMode &= (~IME_CMODE_FULLSHAPE);
ImmSetConversionStatus(vIme, vMode, vSentence);
}
}
}
ImmReleaseContext(AHandle, vIme);
}
private void button1_Click(object sender, EventArgs e)
{
SetHalfShape(textBox1.Handle);
}