/// <summary> /// Specify which control or form we should use for the relative position. /// </summary> public void UseOn(ReflectionTester control) { if (mouseControl == null) { originalPosition = Cursor.Position; } if (control == null) { throw new ArgumentNullException("control"); } mouseControl = new MouseControl(control); PositionUnit = GraphicsUnit.Pixel; // Block any user input while we are active. if (!restoreUserInput) { if (!User32.BlockInput(true)) { //throw new Win32Exception(); } restoreUserInput = true; } }
public static void UnblockInput() { var success = User32.BlockInput(false); if (!success) { var hresult = Marshal.GetHRForLastWin32Error(); throw new ExternalException("Input cannot be unblocked because it was blocked by another thread.", hresult); } }
/// <summary> /// Implements the IDisposable interface. This restores user input. /// It should eventually return the keyboard to its pre-test state. /// </summary> /// <remarks> /// If you are using the Keyboard controller through the base NUnitFormTest /// class, then you should not need to call this method or use finally or using /// blocks. The base class handles this for you.</remarks> public void Dispose() { if (keyboardControl != null) { if (restoreUserInput) { //if this next line returns false, I used to throw an exception... User32.BlockInput(false); restoreUserInput = false; } } }
/// <summary> /// Releases the resources used by the <see cref="MouseController"/>. /// </summary> /// <remarks> /// <b>Dispose</b> releases any pressed mouse keys, restores the /// mouse <see cref="Position"/> and enables user input. /// </remarks> public void Dispose() { if (mouseControl != null) { // If we do not have a control, then an exception will be thrown. try { // Release any pressed mouse buttons. MouseButtons pressedButtons = Control.MouseButtons; if (pressedButtons != MouseButtons.None) { Release(pressedButtons); } // Release any modifier keys. Keys keys = Control.ModifierKeys; if (keys != Keys.None) { Release(keys); } } catch (AmbiguousNameException) { } catch (NoSuchControlException) { } finally { // Restore the mouse position //User32.SetCursorPos(originalPosition.X, originalPosition.Y); Cursor.Position = originalPosition; // Enable user input. if (restoreUserInput) { if (!User32.BlockInput(false)) { //throw new Win32Exception(); } restoreUserInput = false; } } } }
public static bool BlockInput() { var success = User32.BlockInput(true); if (!success) { var hresult = Marshal.GetHRForLastWin32Error(); if (hresult == VSConstants.E_ACCESSDENIED) { Debug.WriteLine("Input cannot be blocked because the system requires Administrative privileges."); } else { Debug.WriteLine("Input cannot be blocked because another thread has blocked the input."); } } return(success); }
/// <summary> /// Initializes the KeyboardController, blocks user input, and sets /// the focus on the specified control. /// </summary> /// <param name="control">The ControlTester to use the keyboard on.</param> public void UseOn(ReflectionTester control) { if (control == null) { throw new ArgumentNullException("control"); } Control c = control.TheObject as Control; FormsAssert.IsTrue(c != null, "Keyboard control requires tester of Control"); sendKeys = sendKeysFactory.Create(c.Handle); keyboardControl = new KeyboardControl(control); if (!restoreUserInput) { //if this next line returns false, I used to throw an exception... User32.BlockInput(true); restoreUserInput = true; } }
/// <summary> /// Blocks user input. Handy if you don't want user to interfere with the application. /// Users can use Ctrl+Alt+Delete to unblock app. /// TO WORK, APP HAS TO RUN WITH ADMINISTRATION PRIVILEGES! /// </summary> /// <param name="block"> /// TRUE to block /// FALSE to unblock /// </param> /// <returns> /// TRUE if blocked /// FALSE if not blocked /// </returns> public static bool BlockInput(bool block) { User32.BlockInput(block); return(isAdministrator()); }
/// <summary> /// Blocks user input /// </summary> /// <param name="blocked">true will block, false will unblock</param> /// <returns>true if the operation succeeded, otherwise false</returns> public static bool BlockInput(bool blocked = true) { return(User32.BlockInput(blocked)); }