如何获得光标下的像素颜色?

Rob*_*cks 4 gdi+ screen pixel visual-c++

我需要一个快速命令行应用程序来返回鼠标光标下的像素颜色.

我怎样才能建立这个在VC++,我需要类似的东西这个,但最好不是在.NET中,因此它可以每秒运行多少次?

Joe*_*oey 12

在我的头顶,直截了当的方式:

#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;
}
Run Code Online (Sandbox Code Playgroud)

ETA:似乎工作,至少对我而言.

ETA2:添加了一些错误检查

ETA3:可以在我的SVN存储库中找到注释代码,已编译的可执行文件和Visual Studio解决方案.