05/08/21 17:18
Valcars92
Salve a tutti e buonasera....ho creato un programmino che trova un colore bitmap sullo schermo prende le coordinate e muove il mouse su di esso premendo il click.... ora quando lo avvio sul desktop e tento di aprire una cartella inserendo il bitmap dell'icona funziona correttamente e non mi da problemi....se invece provo ad aprire un'altra applicazione ad esempio un gioco prendo un bitmap della schermata del gioco e con il gioco aperto avvio il programma mi succede che avvolte funziona correttamente mentre il piu delle volte si blocca senza darmi errore e ne nulla volevo sapere se sbaglio in qualcosa.... ecco lo script
grazie in anticipo come sempre
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MacroGame { public partial class form_MacroGamer : Form { //This is a replacement for Cursor.Position in WinForms [System.Runtime.InteropServices.DllImport("user32.dll")] static extern bool SetCursorPos(int x, int y); [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); public const int MOUSEEVENTF_LEFTDOWN = 0x02; public const int MOUSEEVENTF_LEFTUP = 0x04; public form_MacroGamer() { InitializeComponent(); } private void bt_1_Click(object sender, EventArgs e) { //Prendiamo il contenuto della textbox string _tb = tb_1.Text; try { SearchPixel(_tb); } catch(Exception ex) { MessageBox.Show(ex.ToString()); return; } } private bool SearchPixel(string hexcode) { //Crea un immagine vuota //Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Bitmap bitmap = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height); //Crea un nuovo oggetto graphi per catturare lo screen Graphics graphics = Graphics.FromImage(bitmap as Image); //Screenshot moment -> screen contento to graphics object graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size); Color desiredPixelColor = ColorTranslator.FromHtml(hexcode); for (int x = 0; x < SystemInformation.VirtualScreen.Width; x++) { for (int y = 0; y < SystemInformation.VirtualScreen.Height; y++) { //Get current pixel Color currentPixelColor = bitmap.GetPixel(x, y); if (desiredPixelColor == currentPixelColor) { MessageBox.Show(String.Format("Trovato Pixel a {0},{1} - Adesso set mouse cursor", x, y)); try { LeftMouseClick(x, y); System.Threading.Thread.Sleep(250); LeftMouseClick(x, y); } catch(Exception ex) { MessageBox.Show(ex.ToString()); return false; } return true; } } } return false; } //This simulates a left mouse click public static void LeftMouseClick(int xpos, int ypos) { SetCursorPos(xpos, ypos); mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0); } } }
grazie in anticipo come sempre