Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.80/5 (4 votes)
See more:
Trying to write a bot for online games, at this stage of my task is to determine the color of the monster , hover over it and pressing a double click . All this I learned to do on the desktop by means of creating a loop that iterates through the pixels after which the mouse moves over this pixel reads color and if satisfies twice clicks in place. But as soon as the mouse comes to the game window it simply pereprygivaet.Ya understand is why this is happening because the game is controlled by its own code , but I can still control the mouse in the game? I was just in the first write something like ...

C#
public partial class Form1 : Form
   {
       [DllImport("user32.dll")]
       static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

       [DllImport("gdi32.dll")]
       static extern IntPtr CreateDC(string strDriver,
         string strDevice, string strOutput, IntPtr pData);

       [DllImport("user32.dll")]
       static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

       [DllImport("gdi32.dll")]
       static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

       [DllImport("gdi32.dll")]
       static extern bool DeleteDC(IntPtr hdc);

       static public System.Drawing.Color GetPixelColor(int x, int y)
       {
           IntPtr hdc = CreateDC("Display", null, null, IntPtr.Zero);
           uint pixel = GetPixel(hdc, x, y);
           ReleaseDC(IntPtr.Zero, hdc);
           Color color = Color.FromArgb((int)(pixel & 0x000000FF),
                        (int)(pixel & 0x0000FF00) >> 8,
                        (int)(pixel & 0x00FF0000) >> 16);
           DeleteDC(hdc);
           return color;
       }

       public Form1()
       {
           InitializeComponent();
           Visible = false;
       }

       private void Form1_Load(object sender, EventArgs e)
       {

       }
       [Flags]
       public enum MouseEventFlags
       {
           LEFTDOWN = 0x00000002,
           LEFTUP = 0x00000004,
           MIDDLEDOWN = 0x00000020,
           MIDDLEUP = 0x00000040,
           MOVE = 0x00000001,
           ABSOLUTE = 0x00008000,
           RIGHTDOWN = 0x00000008,
           RIGHTUP = 0x00000010
       }


       private void button1_Click(object sender, EventArgs e)
       {
           Bitmap  pix = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);

           this.Hide();
           Win32.POINT p = new Win32.POINT();
           int check = 0;
           while (check!=3)
           {
               for (int i = 0; i < 768; i += 3)
               {
                   for (int j = 0; j < 1366; j+=7)
                   {
                       Color color = GetPixelColor(p.x, p.y);
                       p.x = Convert.ToInt16(j);
                       p.y = Convert.ToInt16(i);
                       Win32.SetCursorPos(p.x, p.y);
                       if (Color.FromArgb(222, 11, 11) == color)
                       {
                           MessageBox.Show("1");
                           Thread.Sleep(3000);
                           Win32.ClientToScreen(Handle, ref p);

                           mouse_event((int)(MouseEventFlags.LEFTDOWN), p.x, p.y, 0, 0);
                           mouse_event((int)(MouseEventFlags.LEFTUP), p.x, p.y, 0, 0);

                       }
                   }
               }
               check++;
           }
Posted
Comments
agent_kruger 1-Jun-14 4:49am    
sir, that is an interesting question but i think you have to research a lot about it as it is a long process.

1 solution

C# is not the best solution to write bots.
I once have written one using two executables:
The UI and the graphical part has been written in VB (C#'s the same) and the bot core has been written with AutoIt. It you google autoit, you'll find everything you need. That's a Basic derived language which allows you to make operations like clicks and button presses automatic.
Try to google it. If you need i can give u the code of my bot.

AutoIt can also read pixels om screen with easy functions. It can also interact with controls.
I hope this helps.

Jymmy097
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900