c# 关闭中文输入法(系统级别)/开启中文输入法

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

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

        private static uint WM_INPUTLANGCHANGEREQUEST = 0x0050;
        private static int HWND_BROADCAST = 0xffff;
        private static string en_US = "00000409"; //英文
        private static string cn_ZH = "00000804";
        private static uint KLF_ACTIVATE = 1;

        //调用此方法时,将屏蔽中文输入法(操作系统级别,即使使用快捷键ctrl+shift也还原不回中文输入法)
        private static void ChangeLanguage()
        {
            PostMessage(HWND_BROADCAST, WM_INPUTLANGCHANGEREQUEST, IntPtr.Zero, LoadKeyboardLayout(en_US, KLF_ACTIVATE));
        }

        //调用此方法时,将可以使用中文输入法(中文输入法有效)
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            PostMessage(HWND_BROADCAST, WM_INPUTLANGCHANGEREQUEST, IntPtr.Zero, LoadKeyboardLayout(cn_ZH, KLF_ACTIVATE));
            System.Windows.Forms.MessageBox.Show("success");
        }

你可能感兴趣的:(c# 关闭中文输入法(系统级别)/开启中文输入法)