Matthew Mackday Matthew Mackday - 10 months ago 140
C++ Question

COLORREF as RGB or hexadecimal

I have my current code that reads the getPixel from the current cursor position but the return value is just some insane value by COLORREF and I want it to be in RGB. I looked into Microsoft Reference and I found RGB Macro

BYTE byRed,
BYTE byGreen,
BYTE byBlue

and my question is how should I use the return from GetPixel into this RGB function and then print the value?
Current code:

include <Windows.h>
include <wingdi.h>
include <iostream>
pragma comment(lib, "gdi32.lib")

int main() {
while (true) {

hDC = GetDC(NULL);
GetCursorPos( & p);
int cx = p.x;
int cy = p.y;
std::cout << GetPixel(hDC,cx,cy) << std::endl;



Answer Source

The GetPixel() function returns a COLORREF which you can convert to a RGB value like this:

COLORREF color = GetPixel(hdc, x, y);
RGB rgb;

rgb.byRed = GetRValue(color);
rgb.byGreen = GetGValue(color);
rgb.byBlue = GetBValue(color);