111,094
社区成员




public class WR_Imm32
{
public enum GCL
{
CONVERSION = 1,
REVERSECONVERSION = 2,
REVERSE_LENGTH = 3
}
[DllImport("Imm32.dll", CharSet=CharSet.Auto, EntryPoint="ImmGetContext")]
public static extern IntPtr ImmGetContext(IntPtr hWnd);
[DllImport("User32.dll", CharSet=CharSet.Auto, EntryPoint="GetKeyboardLayout")]
public static extern IntPtr GetKeyboardLayout(int idThread);
[DllImport("Imm32.dll", CharSet=CharSet.Auto, EntryPoint="ImmGetConversionList")]
public static extern int ImmGetConversionList(IntPtr hKL, IntPtr hIMC, string lpSrc, IntPtr lpDst, int dwBufLen, int uFlag);
[DllImport("Imm32.dll", CharSet=CharSet.Auto, EntryPoint="ImmReleaseContext")]
public static extern bool MessageBox(IntPtr hWnd, IntPtr hIMC);
[DllImport("imm32.dll", EntryPoint = "ImmInstallIME")]
public static extern int ImmInstallIMEA(string lpszIMEFileName, string lpszLayoutText);
[StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public class CANDIDATELIST
{
public int dwSize;
public int dwStyle;
public int dwCount;
public int dwSelection;
public int dwPageStart;
public int dwPageSize;
public int dwOffset;
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = GetReverseConversion(textBox1.Text);
}
private string GetReverseConversion(string AText)
{
if (AText == "")
return "";
IntPtr m_hKL = WR_Imm32.GetKeyboardLayout(0);
IntPtr m_hIMC = WR_Imm32.ImmGetContext(Handle);
WR_Imm32.CANDIDATELIST m_list = new WR_Imm32.CANDIDATELIST();
int dwSize = WR_Imm32.ImmGetConversionList(m_hKL, m_hIMC, AText, IntPtr.Zero, 0, (int)WR_Imm32.GCL.REVERSE_LENGTH);
IntPtr BufList = Marshal.AllocHGlobal(dwSize);
WR_Imm32.ImmGetConversionList(m_hKL, m_hIMC, AText, BufList, dwSize, (int)WR_Imm32.GCL.REVERSECONVERSION);
Marshal.PtrToStructure(BufList, m_list);
byte[] buf = new byte[dwSize];
Marshal.Copy(BufList, buf, 0, dwSize);
Marshal.FreeHGlobal(BufList);
int os = m_list.dwOffset;
string str = System.Text.Encoding.Unicode.GetString(buf, os, buf.Length - os - 3);
string[] ret = str.Split(new string[] { "\0" }, StringSplitOptions.None);
return ret[0];
}