using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading.Tasks; namespace CCDCount.DLL.Tools { public static class OnScreenKeyboard { [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr hWnd); const int SW_SHOW = 5; const int SW_RESTORE = 9; const int SW_HIDE = 0; public static async void Show() { // 查找现有窗口 IntPtr keyboardWnd = FindWindow("IPTip_Main_Window", null); // 首先确保没有旧进程 KillTabTipProcess(); // 等待进程完全终止 await Task.Delay(300); // 启动新实例 if (StartNewInstance()) { // 等待窗口出现(使用循环检测,而非固定延迟) for (int i = 0; i < 10; i++) { await Task.Delay(200); keyboardWnd = FindWindow("IPTip_Main_Window", null); if (keyboardWnd != IntPtr.Zero) break; } if (keyboardWnd != IntPtr.Zero) { ShowWindow(keyboardWnd, SW_RESTORE); SetForegroundWindow(keyboardWnd); } } } private static bool StartNewInstance() { try { string[] keyboardPaths = { @"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe", @"C:\Program Files (x86)\Common Files\Microsoft Shared\ink\TabTip.exe" }; foreach (string path in keyboardPaths) { if (System.IO.File.Exists(path)) { Process.Start(new ProcessStartInfo { FileName = path, UseShellExecute = true }); return true; } } } catch { // 启动失败 } return false; } public static void Hide() { IntPtr keyboardWnd = FindWindow("IPTip_Main_Window", null); if (keyboardWnd != IntPtr.Zero) { ShowWindow(keyboardWnd, SW_HIDE); } KillTabTipProcess(); } public static void KillTabTipProcess() { try { Process[] processes = Process.GetProcessesByName("TabTip"); foreach (Process process in processes) { process.Kill(); process.WaitForExit(); } } catch { // 忽略异常 } } } }