簡體   English   中英

獲取特定 x,y 坐標處像素的顏色,然后執行單擊

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

我最近進入了 C++,基本上是通過看視頻和逆向工程其他人的代碼來學習的。 不用說,我還不是很擅長這些東西。

無論如何,我正在開發一個程序,該程序檢測特定顏色(RGB)是否位於另一個 window 中的特定坐標處,如果是,則代碼執行單擊。 這是我放在一起的

#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;
}

代碼可以編譯,但即使整個屏幕為藍色或RGB(0,0,255)也不會單擊。 我知道BigClick()點擊,因為我自己測試了它以確保。 我在這里想念什么? 我在想我沒有給GetPixel以正確方式檢查的坐標,但由於我太新了,據我所知,它可能是任何東西。

我更改了固定坐標以跟隨 cursor position 並且您的程序似乎工作正常。 點擊也被執行!

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