| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | void CSearchEdit::ActiveENInput() {     //保存当前输入法     HWND   hTop;     DWORD   ThreadId;     hTop = ::GetForegroundWindow();     ThreadId = GetWindowThreadProcessId(hTop,NULL);     m_Oldhkl = GetKeyboardLayout(ThreadId);     //激活英文输入法     HKL hkl;     hkl=LoadKeyboardLayout(_T("0x0409"),KLF_ACTIVATE);     //装载输入法     if(hkl==NULL)             return;     ActivateKeyboardLayout(hkl,KLF_SETFORPROCESS);     //激活输入法 } ActivateKeyboardLayout(m_Oldhkl,KLF_SETFORPROCESS); //激活原来的输入法 | 
https://blog.csdn.net/chenzhisi/article/details/40864923
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | using System; using System.Diagnostics; using System.Runtime.InteropServices; public class Win32Help {     private delegate bool Wndenumproc(IntPtr hwnd, uint lParam);     [DllImport("user32.dll", SetLastError = true)]     private static extern bool EnumWindows(Wndenumproc lpEnumFunc, uint lParam);     [DllImport("user32.dll", SetLastError = true)]     private static extern IntPtr GetParent(IntPtr hWnd);     [DllImport("user32.dll")]     private static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);     [DllImport("kernel32.dll")]     private static extern void SetLastError(uint dwErrCode);     /// <summary>     /// 获取当前进程的窗口句柄     /// </summary>     /// <returns></returns>     public static IntPtr GetProcessWnd()     {         var ptrWnd = IntPtr.Zero;         var pid = (uint)Process.GetCurrentProcess().Id;  // 当前进程 ID         var bResult = EnumWindows(delegate (IntPtr hwnd, uint lParam)         {             uint id = 0;             if (GetParent(hwnd) != IntPtr.Zero)                 return true;             GetWindowThreadProcessId(hwnd, ref id);             if (id != lParam)                 return true;             ptrWnd = hwnd;   // 把句柄缓存起来             SetLastError(0);    // 设置无错误             return false;   // 返回 false 以终止枚举窗口         }, pid);         return (!bResult && Marshal.GetLastWin32Error() == 0) ? ptrWnd : IntPtr.Zero;     }     [DllImport("imm32.dll")]     private static extern IntPtr ImmGetContext(IntPtr hwnd);     [DllImport("imm32.dll")]     private static extern bool ImmGetOpenStatus(IntPtr himc);     [DllImport("imm32.dll")]     private static extern bool ImmSetOpenStatus(IntPtr himc, bool b);     /// <summary>     /// 设置输入法状态     /// </summary>     /// <param name="tf"></param>     public static void SetImeEnable(bool tf)     {         var handle = GetProcessWnd();         var hIme = ImmGetContext(handle);         ImmSetOpenStatus(hIme, tf);     }     /// <summary>     /// 获取输入法状态     /// </summary>     /// <returns></returns>     public bool GetImeStatus()     {         var handle = GetProcessWnd();         var hIme = ImmGetContext(handle);         return ImmGetOpenStatus(hIme);     } } | 
https://www.jianshu.com/p/75f395cd5dab
 
                    
发表回复