【问题标题】:cant get pixel color of window c++无法获得窗口c ++的像素颜色
【发布时间】:2019-09-05 07:31:07
【问题描述】:

我是使用 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;
}

【问题讨论】:

    标签: c++ winapi


    【解决方案1】:

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

    它对我有用!

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多