如何正确关闭C#winform的屏幕键盘进程?

我目前正在设计一个图像查看器,允许用户输入她的电子邮件并以数字方式获取图像。 困扰我的部分是让屏幕键盘关闭。 我用这段代码启动windows进程:

string progFiles = @"C:\Program Files\Common Files\Microsoft Shared\ink"; string keyboardPath = Path.Combine(progFiles, "TabTip.exe"); Process keyboardProc = Process.Start(keyboardPath); 

之后我打开一个VB InputBox来提示输入电子邮件地址(我使用屏幕键盘,因为应用程序将显示在触摸屏上)。 在此提示后,我想自动关闭该过程。

我试图用以下方法关闭该过程:

 keyboardProc.Kill(); keyboardProc.Dispose(); keyboardProc.Close(); keyboardProc = null; 

它们都不起作用,只是抛出exception:

 An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll Additional information: Cannot process request because the process has exited. 

我也尝试通过ID识别进程并以这种方式关闭它,也没用。 我也看了一下: C#/ .NET:关闭主窗口外的另一个进程但是没有让它工作…… 🙁

我对C#很陌生,这是我第一次从代码中调用Windows进程 – 我错过了什么?

非常感谢你提前!

你能做到:

  Process process = new Process(); process.StartInfo.FileName = progFiles;//Filename process.Start(); process.Close(); 

我也遇到了麻烦。 出于某种原因,这可行,但它会杀死所有打开的TabTip进程。

  //Kill all on screen keyboards Process[] oskProcessArray = Process.GetProcessesByName("TabTip"); foreach (Process onscreenProcess in oskProcessArray) { onscreenProcess.Kill(); } 
  ///  /// Show the On Screen Keyboard ///  #region ShowOSK public static void ShowOnScreenKeyboard() { ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe"); Process.Start(startInfo); } #endregion ShowOSK ///  /// Hide the On Screen Keyboard ///  #region HideOSK public static void HideOnScreenKeyboard() { uint WM_SYSCOMMAND = 274; uint SC_CLOSE = 61536; IntPtr KeyboardWnd = FindWindow("IPTip_Main_Window", null); PostMessage(KeyboardWnd.ToInt32(), WM_SYSCOMMAND, (int)SC_CLOSE, 0); } #endregion HideOSK 

与直接执行/关闭TabTip.exe相比,以下代码将以最快的速度打开/关闭Window 10上的Touch Keyboard。 (在低功率平板电脑上测试)。

  [return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll", SetLastError = true)] static extern bool PostMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern IntPtr FindWindow(String sClassName, String sAppName); [DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, String lpszClass, String lpszWindow); ///  /// Show the On Screen Keyboard ///  #region ShowOSK public static void ShowOnScreenKeyboard() { IntPtr parent = FindWindow("Shell_TrayWnd", null); IntPtr child1 = FindWindowEx(parent, IntPtr.Zero, "TrayNotifyWnd", ""); IntPtr keyboardWnd = FindWindowEx(child1, IntPtr.Zero, null, "Touch keyboard"); uint WM_LBUTTONDOWN = 0x0201; uint WM_LBUTTONUP = 0x0202; UIntPtr x = new UIntPtr(0x01); UIntPtr x1 = new UIntPtr(0); IntPtr y = new IntPtr(0x0240012); PostMessage(keyboardWnd, WM_LBUTTONDOWN, x, y); PostMessage(keyboardWnd, WM_LBUTTONUP, x1, y); } #endregion ShowOSK ///  /// Hide the On Screen Keyboard ///  #region HideOSK public static void HideOnScreenKeyboard() { uint WM_SYSCOMMAND = 0x0112; UIntPtr SC_CLOSE = new UIntPtr(0xF060); IntPtr y = new IntPtr(0); IntPtr KeyboardWnd = FindWindow("IPTip_Main_Window", null); PostMessage(KeyboardWnd, WM_SYSCOMMAND, SC_CLOSE, y); } #endregion HideOSK