如先前的帖子所述,您需要从Win32 API中获取GetPixel函数。
GetPixel位于gdi32.dll内部,因此如果您已经正确设置了环境,您应该可以包含windows.h(它包括wingdi.h)并且一切都应该很好。
如果由于某种原因您只有最小的环境设置,您也可以直接在gdi32.dll上使用LoadLibrary。
GetPixel的第一个参数是设备上下文句柄,可通过调用GetDC函数(也可以通过<windows.h>
获得)来检索。
以下是打印当前光标位置像素颜色的基本示例,其中加载了来自dll的GetPixel:
#include<windows.h>
#include<stdio.h>
typedef WINAPI COLORREF (*GETPIXEL)(HDC, int, int);
int main(int argc, char** argv)
{
HINSTANCE _hGDI = LoadLibrary("gdi32.dll");
if(_hGDI)
{
while(true) {
GETPIXEL pGetPixel = (GETPIXEL)GetProcAddress(_hGDI, "GetPixel");
HDC _hdc = GetDC(NULL);
if(_hdc)
{
POINT _cursor;
GetCursorPos(&_cursor);
COLORREF _color = (*pGetPixel) (_hdc, _cursor.x, _cursor.y);
int _red = GetRValue(_color);
int _green = GetGValue(_color);
int _blue = GetBValue(_color);
printf("Red: 0x%02x\n", _red);
printf("Green: 0x%02x\n", _green);
printf("Blue: 0x%02x\n", _blue);
}
FreeLibrary(_hGDI);
}
}
return 0;
}