1

我编写的程序应该改变程序的布局,这是重点。我有一个获取当前键盘布局 ID 的代码:

const int KL_NAMELENGTH = 9;
    const uint KLF_ACTIVATE = 1;

    [DllImport("user32.dll")]
    public static extern long LoadKeyboardLayout(string pwszKLID, uint Flags);
    [DllImport("user32.dll")]
    public static extern long GetKeyboardLayoutName(System.Text.StringBuilder pwszKLID);

    public static string getKLName()
    {
        System.Text.StringBuilder name = new System.Text.StringBuilder(KL_NAMELENGTH);
        GetKeyboardLayoutName(name);
        return name.ToString();
    }

例如:EN - “00000409” DE- “00000407” FR- “0000040C”

我也有以下代码:

[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);    

 private void ChangeLang(String code)
 {

   PostMessage(GetForegroundWindow(), 0x0050, 2, 0);

 }

现在,调用函数ChangeLang,程序将当前语言切换到下一种语言。但是我需要它来函数 ChangeLang 将消息传递给其他程序,在什么布局上切换(使用代码布局)。这怎么可能?

4

1 回答 1

0
[DllImport("user32.dll")]
private static extern bool PostMessage(IntPtr hhwnd, uint msg, IntPtr wparam, IntPtr lparam);

[DllImport("user32.dll")]
private static extern IntPtr LoadKeyboardLayout(string pwszKLID, uint Flags);

[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();

private const uint WM_INPUTLANGCHANGEREQUEST = 0x0050;
private const uint KLF_ACTIVATE = 1;

private const string en_US = "00000409";

private static void ChangeLanguage(string code)
{
    PostMessage(GetForegroundWindow(), WM_INPUTLANGCHANGEREQUEST, IntPtr.Zero, LoadKeyboardLayout(code, KLF_ACTIVATE));
}
于 2013-06-11T05:16:15.977 回答