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

时间:2019-09-05 07:31:07

标签: c++ winapi

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

1 个答案:

答案 0 :(得分:0)

您的代码可能正在工作(假设您使用正确的窗口名称格式);只是您可能不了解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-然后在记事本中键入一些文本并选择该文本,然后再次运行程序-应该使用不同的颜色!

对我有用!

相关问题