我需要一个快速的命令行应用程序来返回鼠标光标下像素的颜色。我该如何在VC++中构建这个应用程序?我需要类似于这个的东西,但最好不要使用.NET,以便可以每秒运行多次。
就我所知,直接的方式是:
#include <stdio.h>
#include <Windows.h>
int main(void) {
POINT p;
COLORREF color;
HDC hDC;
BOOL b;
// Get the device context for the screen
hDC = GetDC(NULL);
if (hDC == NULL)
return 3;
// Get the current cursor position
b = GetCursorPos(&p);
if (!b)
return 2;
// Retrieve the color at that position
color = GetPixel(hDC, p.x, p.y);
if (color == CLR_INVALID)
return 1;
// Release the device context again
ReleaseDC(GetDesktopWindow(), hDC);
printf("%i %i %i", GetRValue(color), GetGValue(color), GetBValue(color));
return 0;
}
注: 看起来可以运行,至少对我而言如此。
注2: 添加了一些错误检查。
注3: 对代码进行了注释,编译了可执行文件,Visual Studio解决方案可以在我的 SVN 存储库中找到。