简体   繁体   English

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

[英]cant get pixel color of window c++

im beginner in using windows.h library and getting info from windows etc. i have written a code to find the pixel color of any window . 我是使用windows.h库并从Windows等获取信息的初学者。我编写了代码来查找任何窗口的像素颜色。 im not sure what im getting wrong . 我不确定自己在搞错什么。

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

Your code may be working (assuming you have the correct format of the Window name); 您的代码可能正在工作(假设您使用正确的窗口名称格式); it's just that you may not understand the format of a COLORREF object. 只是您可能不了解COLORREF对象的格式。 Try this: 尝试这个:

#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!
}

This shows the three R/G/B values for the pixel colour (255 255 255 is white). 这显示了像素颜色的三个R / G / B值(255 255 255为白色)。

EDIT: Try it, to see if you get 255 255 255 - then type some text in Notepad and select that text, then run your program again - should give a different colour! 编辑:尝试一下,看看是否得到255 255 255然后在记事本中键入一些文本并选择该文本,然后再次运行程序-应该提供不同的颜色!

It works for me! 这个对我有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM