无法从 HWND 获取 getpixel
我试图从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 GetPixel() 文档 :
GetCursorPos()
返回屏幕坐标,但您正在检索子窗口的 HDC。您需要将屏幕坐标转换为相对于子窗口的客户端坐标。您可以使用ScreenToClient()< /code>
或
MapWindowPoints()
为此。Per the GetPixel() documentation:
GetCursorPos()
returns screen coordinates, but you are retrieving anHDC
for a child window. You need to convert the screen coordinates into client coordinates relative to the child window. You can useScreenToClient()
orMapWindowPoints()
for that.