1

I'm doing a program that reads a range of pixels and if find the red color, do a click with left mouse button, but it is too slow, like if the red dot shows up on the range, it takes 2 seconds or more to click.

This is a representation of the problem:

enter image description here

So if some red dot cross the green line, click with left mouse button. The green line do not exist, I drew it just to make it clearer.

Can someone propose a solution to read this interval in milliseconds? I think that the problem is to use a for loop to check the pixel colors.

I tried to use the solution proposed in the topic "Get Pixel color fastest way?" but this made the computer very slow and did not work. (I made some adjustments to adapt the solution to my problem). Also, because my problem is much simpler, I think there is a simpler solution than the one in the other topic, which is more complex and did not work for me.

This is my code:

int main()
{
    HDC dc = GetDC(NULL);
    POINT pt;

    int x = 150;

    while (1) {
        for (int y = 205; y >= 0; y--) {
            dc = GetDC(NULL);
            COLORREF color = GetPixel(dc, x, y);

            // the red color has value = 220
            if (color == 220) {
                GetCursorPos(&pt);
                mouse_event(MOUSEEVENTF_LEFTDOWN, pt.x, pt.y, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTUP, pt.x, pt.y, 0, 0);
            }
        }
    }
    ReleaseDC(NULL, dc);
}
  • 1
    If you're worried about speed I certainly wouldn't call `GetDC` repeatedly inside the loop. Just call it once before the loop (as you are). – john Jul 12 '19 at 05:57
  • Possible duplicate of [Get Pixel color fastest way?](https://stackoverflow.com/questions/10515646/get-pixel-color-fastest-way) – Mailerdaimon Jul 12 '19 at 06:02
  • Thanks John, but that did not make the search faster, it still search for like 2 seconds to find it. – Gabriel Santiago Jul 12 '19 at 16:55
  • Mailerdaimon, I tried to use the solution proposed in the topic "Get Pixel color fastest way?" but this made the computer very slow and did not work. (I made some adjustments to adapt the solution to my problem) – Gabriel Santiago Jul 12 '19 at 16:58

1 Answers1

0

Should you be getting a new image every time you're checking the pixel color? Off the top of my head maybe GETDC should be before the for loop.

while (1) {
        dc = GetDC(NULL);
        for (int y = 205; y >= 0; y--) {

            COLORREF color = GetPixel(dc, x, y);