Color of a screen pixel: Difference between revisions

Content added Content deleted
(added C)
m (→‎{{header|C}}: spaces instead of tabs)
Line 16: Line 16:
{{works with|Windows}}
{{works with|Windows}}
<lang c>COLORREF getColorAtCursor(void) {
<lang c>COLORREF getColorAtCursor(void) {
POINT p;
POINT p;
COLORREF color;
COLORREF color;
HDC hDC;
HDC hDC;
BOOL b;
BOOL b;


// Get the device context for the screen
// Get the device context for the screen
hDC = GetDC(NULL);
hDC = GetDC(NULL);
if (hDC == NULL)
if (hDC == NULL)
return CLR_INVALID;
return CLR_INVALID;


// Get the current cursor position
// Get the current cursor position
b = GetCursorPos(&p);
b = GetCursorPos(&p);
if (!b)
if (!b)
return CLR_INVALID;
return CLR_INVALID;


// Retrieve the color at that position
// Retrieve the color at that position
color = GetPixel(hDC, p.x, p.y);
color = GetPixel(hDC, p.x, p.y);


// Release the device context again
// Release the device context again
ReleaseDC(GetDesktopWindow(), hDC);
ReleaseDC(GetDesktopWindow(), hDC);


return color;
return color;
}</lang>
}</lang>