Hi, I am pretty new to C++ and would really appreciate some help.

I'm trying to write a program that will click on a particular color pixel on my screen. For instance, the program would click on a blue dot on my desktop.

I play a mmorpg and I have been codeing some autos to play the game for me. I've been running my scripts using x,y coordinates to click on the screen. I would like to start being able to use color as an alternative method of clicking. Any help would be greatly appreciated.

I tried using the command FindColor(#color_that_I_Want_To_Click_On) but that keeps coming up with an error.

Code So Far

#include <iostream>
#include <windows.h>
#include <stdlib.h>


using namespace std;

int main()
{
SetConsoleTitle("White Rabbit");
	{
		
                 cout << "Welcome To White Rabbit: Power Miner" << endl;
                 cout << "\n\nPlease Have Your Inventory Open\nYou Should Be Directly South Of The Rock\nAlso Have A North By North East View\nHave Your View As High As Possible" << endl;
                 cout << " " << endl;
      


		int x,y,slpTime,i;

		cout << "Enter the number of ores to mine: " << endl;
		cin >> i;
		cout << "\n\nEnter X Coordinate (255): " << endl;
		cin >> x;
		cout << "\n\nEnter y Coordinate (305): " << endl;
		cin >> y;
		cout << "\n\nEnter Respawn Time For Ore In Seconds: " << endl;
		cin >> slpTime;
		cout << "\n\nStatus." << "\n X coordinate: " << x << " \nY coordinate: " << y << " \nTime in seconds between clicks: " << slpTime << endl;
        for(int t = 1; t <= i; t++){


/*This is where I Need the help instead of (x,y) I need it to locate and click on a color*/
		SetCursorPos(x,y);
		Sleep(1000);
		mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
        Sleep(10000);
        SetCursorPos(565,390);
        Sleep(1000);
		mouse_event(MOUSEEVENTF_RIGHTDOWN, 565, 390, 0, 0);
        mouse_event(MOUSEEVENTF_RIGHTUP, 565, 390, 0, 0);
        Sleep (1000);
        SetCursorPos(565,430);
        Sleep(1000);
		mouse_event(MOUSEEVENTF_LEFTDOWN, 565, 430, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, 565, 430, 0, 0);
		Sleep(slpTime*1000);
        
		if (GetAsyncKeyState(VK_ESCAPE)){
		exit(0);
		}
		else continue;

		}
		return 0;
	}
}

Recommended Answers

All 10 Replies

This will be very dependent on how the game renders the image to the screen. If they use hardware acceleration, it could be difficult. Maybe your game has s function that will allow you to get at this data somehow.

The game is coded in Java and there isn't a way to get to any of the data. I really just need the c++ command that will scan my screen for a certain color then click on it. I've googled it and cant seem to find ANYTHING

Not really =/

Like I said before I'm looking for help making a program that will move the mouse over a pixel color of my choice then click on it.

Find a way to get a screen shot.
Analyze the image for that color.
Use those coordinates.

Is this program using DirectX to render the image you are trying to capture? If so, that may determine which method you need to use to get a screen capture.

This code will get a handle to the window with the title "TITLE".

HWND hTopWindow = FindWindow(NULL, "TITLE");

This code will get a screen capture of the desktop.

Void CaptureScreen()
{
  int    nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  int    nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
  HWND    hDesktopWnd = GetDesktopWindow();
  HDC    hDesktopDC = GetDC(hDesktopWnd);
  HDC  hCaptureDC = CreateCompatibleDC(hDesktopDC);
  HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC, nScreenWidth,   nScreenHeight);
  SelectObject(hCaptureDC,hCaptureBitmap);
  BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,hDesktopDC,0,0,SRCCOPY);
  SaveCapturedBitmap(hCaptureBitmap);                //Place holder - Put your code here to save the captured image to disk
  ReleaseDC(hDesktopWnd,hDesktopDC);
  DeleteDC(hCaptureDC);
  DeleteObject(hCaptureBitmap);
}

I'll leave it up to you to integrate the FindWindow call into this function. There's no guarantee this will actually get the image you want but you can test to see. Once you have the image, you'll have to find a way to analyze it for your color. Then you'll send a click event to the window with those coordinates.

I already know the color code that I want the mouse to get. The problem is the pixel moves around the screen (slowly) so I can't use an x,y coordinate to click it.

Also, there are these sprites used to stop autoers that pop up and ask you to do certain things. I need to be able to check for these by running a command that is constantly checking for those colors on my screen.

After you get the image, analyze it to get the coordinates, could you send multiple click commands to a certain radius around the point to try to get the moving object?

Lol, for the stuff Im thinking of doing later I just really need to be able to find a color pixel on the screen and click it. Im really suprised that its as complicated as this. I havent been able to find help from ANYONE who knows about this =(

I've told you how to do it. I've given you code to start. There are different ways to get the screen capture but you haven't said you have any problems with what I've given you.

If you're looking for a FindColorAndClick(color,x,y) function, you're not going to find it.

The method you most likely need is:
Get caputre.
Analyze for color.
Send click at color.

I've given you some code to get a start on number 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.