private void EnsureDestroyed() { if (_timer is not null) { _timer.Dispose(); _timer = null; } if (_tipWindow is not null) { _tipWindow.DestroyHandle(); _tipWindow = null; } // Hide the window and invalidate the parent to ensure // that we leave no visual artifacts. given that we // have a bizarre region window, this is needed. User32.SetWindowPos( new HandleRef(this, Handle), User32.HWND_TOP, _windowBounds.X, _windowBounds.Y, _windowBounds.Width, _windowBounds.Height, User32.SWP.HIDEWINDOW | User32.SWP.NOSIZE | User32.SWP.NOMOVE); _parent?.Invalidate(true); DestroyHandle(); }
// will destroy the tipWindow public void Destroy() { Debug.Assert(tipWindow != null, "how can one destroy a null window"); tipWindow.DestroyHandle(); tipWindow = null; }
//============================================================== /// <summary> /// 通过向指定的按键发送KeyUp来恢复状态 /// </summary> /// <param name="c"></param> /// <param name="allKeys"></param> public static void TryResetKeys(params IEnumerable<VirtualKeyCode>[] allKeys) { var dummyWnd = new NativeWindow(); try { //如果被系统或者杀毒软件阻止, 则发送给自己的一个窗口,这样避免被在此拦截。 dummyWnd.CreateHandle(new CreateParams(){ExStyle = (int) (User32.WS_EX.WS_EX_LAYERED | User32.WS_EX.WS_EX_TOOLWINDOW)}); User32.ShowWindow(dummyWnd.Handle, User32.SW.SW_SHOWNORMAL); var sim = new InputSimulator(); sim.Keyboard.Sleep(10); foreach (var keys in allKeys) { foreach (var k in keys) { User32.SetForegroundWindow(dummyWnd.Handle); sim.Keyboard.KeyUp(k); } } } catch (Exception) { Debug.WriteLine("恢复键盘状态失败"); #if TEST throw; #endif } finally { dummyWnd.DestroyHandle(); } }