C++ 尝试在鼠标点读取GetPixel(),始终返回4294967-具有位图和剪裁区域

C++ 尝试在鼠标点读取GetPixel(),始终返回4294967-具有位图和剪裁区域,c++,winapi,C++,Winapi,我正在尝试制作一个程序,在某一点读取像素颜色。我想我已经为我的游戏设置了一个剪辑和位图区域,但是我不知道为什么它不起作用 using namespace std; int main() { while (true) { LPCWSTR window_title = L"World of Warcraft"; HWND hWND = FindWindow(NULL, window_title); RECT rWindow;

我正在尝试制作一个程序,在某一点读取像素颜色。我想我已经为我的游戏设置了一个剪辑和位图区域,但是我不知道为什么它不起作用

using namespace std;

int main() {
    while (true) {
        LPCWSTR window_title = L"World of Warcraft";
        HWND hWND = FindWindow(NULL, window_title);
        RECT rWindow;
        RECT rClient;

        HRGN hRgnWindow;
        HRGN hRgnClient;
        HRGN hNCRgn;

        //C: Get the window and client rectangles for the window.
        GetWindowRect(hWND, &rWindow);
        GetClientRect(hWND, &rClient);

        //C: Translate the Client rectangle into screen coordinates.
        POINT p = { 0,0 };
        MapWindowPoints(hWND, NULL, &p, 1);
        OffsetRect(&rClient, p.x, p.y);

        //C: Create regions from these two rectangles.
        hRgnWindow = ::CreateRectRgnIndirect(&rWindow);
        hRgnClient = ::CreateRectRgnIndirect(&rClient);
        hNCRgn = ::CreateRectRgn(0, 0, 0, 0);

        //C: Subtract the client region from the window region.
        CombineRgn(hNCRgn, hRgnWindow, hRgnClient, RGN_DIFF);

        while (hWND == NULL) {
            hWND = FindWindowA(NULL, "World of Warcraft");
            cout << "start game!" << endl;
            Sleep(1000);
        }
        Sleep(10);
        if (GetAsyncKeyState(VK_SHIFT)) { // MousePosition
            //HDC hDC = GetDC(hWND); 
            POINT p;
            GetCursorPos(&p);
            ScreenToClient(hWND, &p); // actually only needs window title not HDC but will need for color
            //ReleaseDC(hWND, hDC);
            cout << "(" << p.x << ","<< p.y << ")" << endl;
            Sleep(1000);
        }
        Sleep(10);
        if (GetAsyncKeyState(VK_LBUTTON)) { // pixelColor
            HDC hDC, hCDC;
            HBITMAP hbwin;
            HRGN RrGN = CreateRectRgn(30, 5, 1277, 690);
            int height, width;
            hDC = GetDC(hWND);
            hCDC = CreateCompatibleDC(hDC);
            SetStretchBltMode(hCDC, COLORONCOLOR);

            RECT winsize;
            GetClientRect(hWND, &winsize);
            SetWindowRgn(hWND, RrGN, TRUE);
            SelectClipRgn(hDC, RrGN);
            GetClipRgn(hDC, RrGN);

            POINT p;
            GetCursorPos(&p);
            ScreenToClient(hWND, &p);

            hbwin = CreateCompatibleBitmap(hDC, p.x, p.y);
            SelectObject(hCDC, hbwin);

            COLORREF color = GetPixel(hCDC, p.x, p.y);

            cout << " | Color: " << color << endl;
                /*cout<<  //"(" << p.x << "," << p.y << ")" << " R: " << (int)GetRValue(color) 
                << " | G: " << (int)GetGValue(color) << " | B: "
                << (int)GetBValue(color) << endl;
            BOOL EndPaint(hWND, CONST PAINTSTRUCT * lp);*/
            Sleep(10);
            cout << " | Color: " << color << endl;
            DeleteObject(hbwin);
            DeleteDC(hCDC);
            ReleaseDC(hWND, hDC);
            DeleteObject(RrGN);
            Sleep(1000);
        }
        /*if (GetAsyncKeyState(VK_CONTROL)) {
            HDC hDC = GetDC(hWND);
            COLORREF color;                     //could have the choice of which zone or 
            COLORREF characteristic_ color = ; //color of fishing bobber? --> How to find in different zones
            Sleep(1000);
        }*/
        if (GetAsyncKeyState(VK_MENU)) { // Exit Game
            return 0;
        }
        DeleteObject(hRgnWindow);
        DeleteObject(hRgnClient);
        DeleteObject(hNCRgn);
    }
    return 0;
}
使用名称空间std;
int main(){
while(true){
LPCWSTR window_title=L“魔兽世界”;
HWND HWND=FindWindow(空,窗口标题);
雷克尔文多;
直视的;
HRGN-hRgnWindow;
HRGN-hRgnClient;
HRGN-hNCRgn;
//C:获取窗口和窗口的客户端矩形。
GetWindowRect(hWND和rWindow);
GetClientRect(hWND和rClient);
//C:将客户端矩形转换为屏幕坐标。
点p={0,0};
MapWindowPoints(hWND、NULL和p,1);
偏移量(&rClient,p.x,p.y);
//C:从这两个矩形创建区域。
hRgnWindow=::CreateRectRgnIndirect(&rWindow);
hRgnClient=::CreateRectRgnIndirect(&rClient);
hNCRgn=::CreateRectRgn(0,0,0,0);
//C:从窗口区域中减去客户区域。
组合器(hNCRgn、hRgnWindow、hRgnClient、RGN_DIFF);
while(hWND==NULL){
hWND=FindWindowA(NULL,“魔兽世界”);

cout完整返回值为
4294967295‬以十六进制表示的“0xffffffff”(
CLR\u无效
)。
作为注释,调用
CreateCompatibleBitmap
仅创建一个空位图,您需要将内容从
hDC
复制到
hCDC

    hbwin = CreateCompatibleBitmap(hDC, rClient.right- rClient.left, rClient.bottom - rClient.top);
    SelectObject(hCDC, hbwin);

    BitBlt(hCDC, 0, 0, rClient.right - rClient.left, rClient.bottom - rClient.top, hDC, 0, 0, SRCCOPY);
    COLORREF color = GetPixel(hCDC, p.x, p.y);

    cout << " | Color: " << color << endl;
    cout << " | Color: " << color << endl;
hbwin=CreateCompatibleBitmap(hDC,rClient.right-rClient.left,rClient.bottom-rClient.top);
选择对象(hCDC、hbwin);
BitBlt(hCDC,0,0,rClient.right-rClient.left,rClient.bottom-rClient.top,hDC,0,0,srcopy);
COLORREF color=GetPixel(hCDC,p.x,p.y);

cout
CreateCompatibleBitmap
创建一个空位图。我真的不知道你想做什么。你可以通过获取整个屏幕的DC来读取屏幕像素的颜色(
GetDC(NULL)
)然后用屏幕坐标在DC上调用
SetPixel
。这样有帮助吗?@JonathanPotter你的意思是
GetPixel()
,而不是
SetPixel()
,对吗?哦,是的。谢谢。@JonathanPotter谢谢你的回答!我不是在设置像素颜色。我在尝试从游戏中读取像素颜色,并将其转换为RBG格式。我正在尝试首先获取鼠标位置(这很好,但不会使我的游戏窗口的左上角变成0,0,所以可能是因为这个,然后使用像素RBG颜色并将数字输出到我的控制台。您好@v8wr,这回答了您的问题吗?如果您有任何问题,请随时与我联系。