试图在鼠标点读取GetPixel(),总是返回4294967 - 有位图和剪切区域。

问题描述 投票:-3回答:1

我想做一个程序,在某一个点读取像素颜色。我想我已经成功地给我的游戏设置了一个剪切和位图区域,但我就是不知道为什么它不能工作。

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

IInspectable评论说CreateCompatibleBitmap()会生成一个空的位图,但是我如何让它从窗口中获取像素呢?我试过这样做,但我似乎想不通! 谢谢你的帮助!我想做一个程序,让它从窗口中提取像素。

c++ winapi
1个回答
0
投票

完整的返回值是 4294967295‬ 其中十六进制 "0xffffffffff"(CLR_INVALID).作为评论,调用 CreateCompatibleBitmap 只创建一个空的位图,您需要从 hDChCDC.

    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;

此外,使用 GetDIBits 可能比使用 GetPixel你可以参考msdn上的样本。

捕捉图像

和这个问题。

GetDIBits和循环使用X,Y的像素。

© www.soinside.com 2019 - 2024. All rights reserved.