无法获取窗口 C++ 的像素颜色

cant get pixel color of window c++

本文关键字:像素 颜色 C++ 窗口 获取      更新时间:2023-10-16

im 初学者使用 Windows.h 库并从 Windows 等获取信息。 我写了一个代码来查找任何窗口的像素颜色。我不确定我出了什么问题.

#include <iostream>
#include <windows.h>
using namespace std;
COLORREF centerColor;
POINT cent;
int main()
{
HWND hd = FindWindow(NULL, L"Untitled - Notepad");
HDC hdc_ = GetDC(hd); 
cent.x = 0;
cent.y = 0;
centerColor = GetPixel(hdc_, cent.x, cent.y);
cout << centerColor;
}

你的代码可能正在工作(假设你有正确的窗口名称格式(;只是你可能不理解COLORREF对象的格式。试试这个:

#include <iostream>
#include <windows.h>
using namespace std;
COLORREF centerColor;
POINT cent;
int main()
{
HWND hd = FindWindow(NULL, L"Untitled - Notepad");
//  HWND hd = FindWindow(NULL, "Untitled - Notepad"); // Use this version if you are NOT using a Unicode build!
HDC hdc_ = GetDC(hd);
cent.x = 0;
cent.y = 0;
centerColor = GetPixel(hdc_, cent.x, cent.y);
//  cout << centerColor;
cout << int(GetRValue(centerColor)) << " " << int(GetGValue(centerColor)) << " " << int(GetBValue(centerColor)) << endl;
ReleaseDC(hd, hdc_); // You should call this when you've finised with the DC!
}

这显示了像素颜色的三个 R/G/B 值(255 255 255 为白色(。

编辑:试试吧,看看你是否得到255 255 255- 然后在记事本中输入一些文本并选择该文本,然后再次运行你的程序 - 应该给出不同的颜色!

它对我有用!