/// <summary> /// Stop foreground application bot /// </summary> public void StopForeground() { // Get the name of the foreground application string ProcessName = ClickLib.GetForegroundProcessName(); // Get the bot attached to foreach (ClickBot clickBot in ClickBots) { if (clickBot.ProcessName == ProcessName) { clickBot.Stop(); } } }
/// <summary> /// Start the bot /// </summary> public void Start() { // Si déjà démarrer, on sort if (this.IsRunning) { return; } // Récupération du hWnd et de coordonnées this.hWnd = ClickLib.GethWnd(this.ProcessName); this.lParam = ClickLib.lParam(this.X, this.Y); if (this.hWnd == IntPtr.Zero || this.lParam == IntPtr.Zero) { return; } // Démarrage de la tâche this.IsError = false; this.IsRunning = true; this.BotTask = Task.Run(() => Run()); }
/// <summary> /// Bot running /// </summary> private void Run() { try { while (this.IsRunning) { bool downSuccess = ClickLib.PostMessage(this.hWnd, ClickLib.WM_LBUTTONDOWN, 1, this.lParam); Thread.Sleep((int)this.ReleaseDelay); bool upSuccess = ClickLib.PostMessage(this.hWnd, ClickLib.WM_LBUTTONUP, 0, this.lParam); if (!(downSuccess & upSuccess)) { throw new Exception("Can't find hWnd!"); } Thread.Sleep((int)this.Delay); } } catch { this.IsRunning = false; this.IsError = true; } }
/// <summary> /// Add foreground application in list /// </summary> public void AddForeground() { // Get hWnd of the foreground application IntPtr hWnd = ClickLib.GetForegroundWindow(); // Get process name string ProcessName = ClickLib.GetProcessName(hWnd); // Get cursor coordinates ClickLib.RECT rct = new ClickLib.RECT(); ClickLib.GetWindowRect(hWnd, ref rct); int mouserelativepositionX = Control.MousePosition.X - rct.Left; int mouserelativepositionY = Control.MousePosition.Y - rct.Top; // Checking if a bot for this application already exists foreach (ClickBot Bot in ClickBots) { if (Bot.ProcessName == ProcessName) { Bot.X = (uint?)mouserelativepositionX; Bot.Y = (uint?)mouserelativepositionY; return; } } // Creates the bot ClickBot clickBot = new ClickBot(); clickBot.ProcessName = ProcessName; clickBot.X = (uint?)mouserelativepositionX; clickBot.Y = (uint?)mouserelativepositionY; ClickBots.Add(clickBot); }