切换其他程序的键盘布局

时间:2013-05-05 16:50:22

标签: c# wpf

我编写的程序应该改变程序的布局,这是焦点。我有一个代码获取当前键盘布局的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函数传递给其他程序,在什么布局上切换(使用代码布局)。这怎么可能呢?

1 个答案:

答案 0 :(得分: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));
}