无法从 HWND 获取 getpixel

发布于 2025-01-10 08:03:45 字数 1059 浏览 2 评论 0原文

我试图从 hwnd 获取像素颜色

,但它不起作用

当我输入“GetDC(NULL)”时,它起作用了

,但“GetDC(hwnd_child)”始终是 255,255,255。

请帮忙

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

using namespace std;

int main() {

    POINT pos;
    int R;
    int G;
    int B;
    while (1) {    
        HWND  hwnd = FindWindow(NULL, L"LDPlayer"); //FindWindow(NULL, windowTitle);
        HWND hwnd_child = FindWindowEx(hwnd, NULL, L"RenderWindow", L"TheRender");
        HDC deviceContext1 = GetDC(hwnd_child );
        GetCursorPos(&pos);
        COLORREF color = GetPixel(deviceContext1, pos.x, pos.y);
        SetPixel(deviceContext1, pos.x, pos.y, RGB(255, 0, 0));
        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_child, deviceContext1);
      
        Sleep(1000);
    }

    return 0;
}

I am trying to get pixel color from hwnd

but it doesn't work

When I put "GetDC(NULL)" It worked

but "GetDC(hwnd_child)" is always 255,255,255.

please help

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

using namespace std;

int main() {

    POINT pos;
    int R;
    int G;
    int B;
    while (1) {    
        HWND  hwnd = FindWindow(NULL, L"LDPlayer"); //FindWindow(NULL, windowTitle);
        HWND hwnd_child = FindWindowEx(hwnd, NULL, L"RenderWindow", L"TheRender");
        HDC deviceContext1 = GetDC(hwnd_child );
        GetCursorPos(&pos);
        COLORREF color = GetPixel(deviceContext1, pos.x, pos.y);
        SetPixel(deviceContext1, pos.x, pos.y, RGB(255, 0, 0));
        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_child, deviceContext1);
      
        Sleep(1000);
    }

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海夕 2025-01-17 08:03:45

根据 GetPixel() 文档 :

返回值是指定像素 RGB 的 COLORREF 值。 如果像素位于当前剪切区域之外,则返回值为 CLR_INVALID(Wingdi.h 中定义的 0xFFFFFFFF)。

GetCursorPos() 返回屏幕坐标,但您正在检索子窗口的 HDC。您需要将屏幕坐标转换为相对于子窗口的客户端坐标。您可以使用 ScreenToClient()< /code>MapWindowPoints() 为此。

GetCursorPos(&pos);
ScreenToClient(hwnd_child, &pos);
COLORREF color = GetPixel(deviceContext1, pos.x, pos.y);
GetCursorPos(&pos);
MapWindowPoints(NULL, hwnd_child, &pos, 1);
COLORREF color = GetPixel(deviceContext1, pos.x, pos.y);

Per the GetPixel() documentation:

The return value is the COLORREF value that specifies the RGB of the pixel. If the pixel is outside of the current clipping region, the return value is CLR_INVALID (0xFFFFFFFF defined in Wingdi.h).

GetCursorPos() returns screen coordinates, but you are retrieving an HDC for a child window. You need to convert the screen coordinates into client coordinates relative to the child window. You can use ScreenToClient() or MapWindowPoints() for that.

GetCursorPos(&pos);
ScreenToClient(hwnd_child, &pos);
COLORREF color = GetPixel(deviceContext1, pos.x, pos.y);
GetCursorPos(&pos);
MapWindowPoints(NULL, hwnd_child, &pos, 1);
COLORREF color = GetPixel(deviceContext1, pos.x, pos.y);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文