用C++获取屏幕上某点的颜色

假定坐标点 x=50,y=50. 输出 RGB 用 16 进制数。
Afxwin.h 你建项目时可得。
#include <Afxwin.h>
#include <Windows.h>
#pragma comment (lib, "User32.lib")

int main(void){
HWND hWnd = ::GetDesktopWindow();
HDC hdc = ::GetDC(hWnd);
// HDC hdc = ::GetDC(NULL);
int x=50,y=50;
COLORREF pixel = ::GetPixel(hdc, x, y);
if (pixel != CLR_INVALID) {
int red = GetRValue(pixel);
int green = GetGValue(pixel);
int blue = GetBValue(pixel);
printf("R=%x G=%x B=%x\n",red,green,blue);
} else {
printf("outside");
}
system("PAUSE");
return 0;
}

posted @ 2019-07-05 14:02  ꧁执笔小白꧂  阅读(2437)  评论(0编辑  收藏  举报