Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Threading;
- using System.Windows.Forms;
- internal sealed class TestCursorController : IDisposable
- {
- private static int hasMutex;
- private static readonly Mutex CrossProcessMutex = new Mutex(initiallyOwned: false, "Cursor");
- private readonly Point originalPosition;
- private readonly Rectangle originalClip;
- private int isDisposed;
- public static TestCursorController AssertControl() => new TestCursorController();
- private TestCursorController()
- {
- if (Interlocked.Exchange(ref hasMutex, 1) != 0)
- throw new InvalidOperationException($"Tests that use {nameof(TestCursorController)} must be set to not run in parallel with one another.");
- if (!CrossProcessMutex.WaitOne(TimeSpan.FromSeconds(10)))
- throw new TimeoutException("Another process is using the cursor.");
- originalPosition = Cursor.Position;
- originalClip = Cursor.Clip;
- }
- public void Dispose()
- {
- CrossProcessMutex.ReleaseMutex();
- if (Interlocked.Exchange(ref isDisposed, 1) != 0) return;
- Cursor.Clip = originalClip;
- Cursor.Position = originalPosition;
- Volatile.Write(ref hasMutex, 0);
- }
- public void SetPosition(Control control, Point relativePosition)
- {
- if (control is null) throw new ArgumentNullException(nameof(control));
- SetPosition(control.PointToScreen(relativePosition));
- }
- private static void SetPosition(Point screenPosition)
- {
- Cursor.Clip = new Rectangle(screenPosition, new Size(1, 1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement