日期:2014-05-17  浏览次数:20910 次

一个问题郁闷了两天了,请教各位。
查到几个用来实现功能的API,但没有例子,不会用啊, 

ImmGetRegisterWordStyle 
ImmRegisterWord 
ImmUnregisterWord

写了程序,实现失败。有人给些点拨么?

我的代码如下:

[ DllImport( "user32.dll " )] 
public static extern IntPtr LoadKeyboardLayout( String pwszKLID, UInt32 Flags ); 
[ DllImport( "imm32.dll " )] 
public static extern UInt32 ImmGetRegisterWordStyle( IntPtr hKL, UInt32 nItem, [In, Out] STYLEBUF[] lpStyleBuf ); 

private void Load(object sender, System.EventArgs e) 

STYLEBUF[] stlBuff = new STYLEBUF[1]; 
IntPtr hKL = LoadKeyboardLayout( "E0010411 ", 1); 

UInt32 num = ImmGetRegisterWordStyle(hKL, 0, stlBuff); 

STYLEBUF[] stlBuff1 = new STYLEBUF[num]; 
UInt32 rc = ImmGetRegisterWordStyle(hKL, num, stlBuff1); 



public struct STYLEBUF 

Int32 dwStyle; 
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 )]  
String szDescription; 
}

红色部分总是获取不到值,ImmRegisterWord 也总是返回false。不知道哪里写的有问题

------解决方案--------------------
日文输入法怎么弄我也不知道,估计你的错还是HKL没有填对。HKL的高16位是键盘的物理布局,低16位语言标识(Locale ID),中文是2052,日文是1041(0x0411)。
你可以用下面的方法看下系统中到底装了哪些输入法:
C# code

    [DllImport("user32.dll")] public static extern int GetKeyboardLayoutList(int size, [Out, MarshalAs(UnmanagedType.LPArray)] IntPtr[] hkls);
    [DllImport("imm32")] public static extern uint ImmGetDescription(IntPtr hKL, StringBuilder lszDescription, uint uBufLen);
    [DllImport("imm32")] public static extern uint ImmGetIMEFileName(IntPtr hKL, StringBuilder lpszFileName, uint uBufLen);

    static void Main(string[] args)
    {
        IntPtr[] hkls = new IntPtr[20];
        int n = GetKeyboardLayoutList(20, hkls);

        Console.WriteLine("{0} layouts:", n);
        for (int i = 0; i < n; i++)
        {
            IntPtr hkl = hkls[i];

            var desc = new StringBuilder(500);
            ImmGetDescription(hkl, desc, 500);

            var fname = new StringBuilder(500);
            ImmGetIMEFileName(hkl, fname, 500);

            Console.WriteLine("{0}({1}) '{2}' ({3})", hkl.ToString("x"), (int)hkl & 0x00ffff, desc, fname);
        }
    }