I very recently got into C++, and am basically learning through watching videos and reverse-engineering other people's code. Needless to say, I'm not very good at this stuff yet.
Anyway, I'm working on a program that detects if a specific color (RGB) is at a specific coordinate in another window, and if it is, the code executes a click. This is what I put together
#include <windows.h>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
void BigClick()
{
INPUT Input = { 0 };
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; //push
::SendInput(1, &Input, sizeof(INPUT));
::ZeroMemory(&Input, sizeof(INPUT)); //or NULL?
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP; //release
::SendInput(1, &Input, sizeof(INPUT));
}
int main()
{
COLORREF colortosearch = RGB(0, 0, 255); // color to search
HDC hdcScreen = GetDC(NULL);
int x = 954;
int y = 540;
while (true)
{
if (::GetPixel(hdcScreen, x, y) == colortosearch)
{
// start code to click
cout << "one click completed";
BigClick();
}
}
::ReleaseDC(NULL, hdcScreen);
return 0;
}
The code compiles but it does not click even when the entire screen is blue or RGB(0,0,255)
. I know BigClick()
clicks, since I tested it by itself to make sure. What am I missing here? I'm thinking I'm not giving GetPixel
the coordinates to check in the right way, but since I'm so new it could be anything as far as I know.