HÀM GetPixel khi sử dụng cho 1 handle của một ứng dụng thì lỗi

mình có sử dụng 2 hàm main như bên dưới, hàm Main1 thì lấy đúng mã màu RGB của tất cả các điểm trên màn hình, nhưng khi mình dùng hàm main2 thì nó vẫn lấy đúng toạ độ của điểm theo ứng dụng mình cần lấy nhưng lại trả về sai giá trị RGB
==> Xin các bác hỗ trợ giúp cách fix lỗi này

 #include <iostream>
#include <Windows.h>

using namespace std;

int main1() {
    POINT pos;
    int R;
    int G;
    int B;
    while (1) {
        GetCursorPos(&pos);
        HDC hDC = GetDC(NULL);
        COLORREF color = GetPixel(hDC, pos.x, pos.y);
        R = GetRValue(color);
        G = GetGValue(color);
        B = GetBValue(color);
        std::cout << "x : " << pos.x << ", y : " << pos.y << ", R : " << R << ", G : " << G << ", B : " << B << endl;
        ReleaseDC(NULL, hDC);
        Sleep(1000);
    }

    return 0;
}
int main2() {
    POINT pos;
    int R;
    int G;
    int B;
    while (1) {
        LPCWSTR window_title = L"HxD";
        HWND hWND = FindWindow(NULL, window_title);
        while (!hWND)
        {
            hWND = FindWindow(NULL, window_title);
            cout << "not found window"<<endl;
            Sleep(1000);
        }
        GetCursorPos(&pos);
        ScreenToClient(hWND, &pos);
        HDC hDC = GetDC(hWND);
        COLORREF color = GetPixel(hDC, pos.x, pos.y);
        R = GetRValue(color);
        G = GetGValue(color);
        B = GetBValue(color);
        std::cout << "x : " << pos.x << ", y : " << pos.y << ", R : " << R << ", G : " << G << ", B : " << B << endl;
        ReleaseDC(hWND, hDC);
        Sleep(1000);
    }
    return 0;
}

Có thể do DC không hỗ trợ hàm GetPixel. Bạn nên gọi hàm GetDeviceCaps để kiểm tra xem device context đó có hỗ trợ hàm này không. Nếu không hỗ trợ thì mọi tọa độ của DC đều trả về CLR_INVALID (0xffffffff).

Nó không hỗ trợ thì bạn phải dùng cách cho toàn màn hình thôi.

3 Likes

mình đành chấp nhận với giải pháp này, mặc dù chưa đúng yêu cầu nhưng có lẽ getpixel nó ko chơi với getDC

#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
    POINT pos;
    int R;
    int G;
    int B;

    while (1) {
        LPCWSTR window_title = L"HxD";
        HWND hWND = FindWindow(NULL, window_title);
        while (!hWND)
        {
            hWND = FindWindow(NULL, window_title);
            cout << "not found window" << endl;
            Sleep(1000);
        }
        GetCursorPos(&pos);
        HWND hWndxy = WindowFromPoint(pos);
        ScreenToClient(hWND, &pos);
        if (pos.x >= 0 && pos.y >= 0)
        {
            HDC hDC = GetDC(hWndxy);
            COLORREF color = GetPixel(hDC, pos.x, pos.y);
            R = GetRValue(color);
            G = GetGValue(color);
            B = GetBValue(color);
            std::cout << "x : " << pos.x << ", y : " << pos.y << ", R : " << R << ", G : " << G << ", B : " << B << endl;
            ReleaseDC(hWndxy, hDC);
        }
        Sleep(1000);
    }
    return 0;
}
2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?