0

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Have you tried stepping through the execution using a debugger to see whether the values you get from your winapi calls are what you expect? – UnholySheep Aug 18 '20 at 08:11
  • If not for debugger, then could you print values of `::GetPixel(hdcScreen, x, y)` in console, because it might actually have value CLR_INVALID `0xFFFFFFFF`, if you request incorrectly – Alexey S. Larionov Aug 18 '20 at 08:35
  • I think there is no problem with your code, luffy's answer has been able to help you test. I think the problem lies in `but it does not click even when the entire screen is blue`, maybe the entire screen is blue but it is not `RGB(0,0,255)` – Zeus Aug 18 '20 at 09:32
  • @themasterofcomedy209 "*am basically learning through watching videos and reverse-engineering other people's code*" - get yourself some [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. – Remy Lebeau Aug 18 '20 at 16:08
  • [Is it a bug to pass a single-element array to SendInput?](https://stackoverflow.com/q/46744894/1889329) That's what you get for copying other people's code, without understanding it. You're just blindly copying bugs. Anyway, I cannot reproduce your issue. – IInspectable Aug 18 '20 at 21:25

1 Answers1

1

I changed the fixed coordinates to follow the cursor position and your program seems to work fine. Clicks are also executed!

int main()
{
    COLORREF colortosearch = RGB(0, 0, 255); // color to search
    HDC hdcScreen = GetDC(NULL);

    while (true)
    {
        POINT cursor;
        GetCursorPos(&cursor);
        COLORREF color = ::GetPixel(hdcScreen, cursor.x, cursor.y);

        if (color == colortosearch) {
            // start code to click
            cout << "one click completed";
            BigClick();
        }
        else {
            int red = GetRValue(color);
            int green = GetGValue(color);
            int blue = GetBValue(color);
            cout << "x: " << cursor.x << ", y:" << cursor.y << " --> ";
            cout << "(" << red << ", " << green << ", " << blue << ")\r\n";
        }
    }

    ::ReleaseDC(NULL, hdcScreen);

    return 0;
}

enter image description here

luffy
  • 2,246
  • 3
  • 22
  • 28