Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 切换其他程序的键盘布局_C#_Wpf - Fatal编程技术网

C# 切换其他程序的键盘布局

C# 切换其他程序的键盘布局,c#,wpf,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")] publi

我写的程序应该改变程序的布局,这是重点。我有一个获取当前键盘布局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,程序将当前语言切换到下一种语言。但我需要它来实现将消息传递给其他程序的功能,在什么布局开关上(使用代码布局)。这怎么可能呢

[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));
}