简体   繁体   English

获取特定 x,y 坐标处像素的颜色,然后执行单击

[英]Getting the color of a pixel at a specific x,y coordinate, then executing a click

I very recently got into C++, and am basically learning through watching videos and reverse-engineering other people's code.我最近进入了 C++,基本上是通过看视频和逆向工程其他人的代码来学习的。 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.无论如何,我正在开发一个程序,该程序检测特定颜色(RGB)是否位于另一个 window 中的特定坐标处,如果是,则代码执行单击。 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) .代码可以编译,但即使整个屏幕为蓝色或RGB(0,0,255)也不会单击。 I know BigClick() clicks, since I tested it by itself to make sure.我知道BigClick()点击,因为我自己测试了它以确保。 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.我在想我没有给GetPixel以正确方式检查的坐标,但由于我太新了,据我所知,它可能是任何东西。

I changed the fixed coordinates to follow the cursor position and your program seems to work fine.我更改了固定坐标以跟随 cursor position 并且您的程序似乎工作正常。 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;
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM