Click to See Complete Forum and Search --> : pixel value of covered window


dave2k
November 14th, 2005, 11:42 AM
so far this works, but was wondering whether there is a more code-efficient way of getting the value of a pixel from a window, even if it's covered?

::GetWindowRect(hWnd, &rc);
int w = rc.right-rc.left;
int h = rc.bottom-rc.top;

HDC hdc = ::GetDC(0);
HDC memDC = ::CreateCompatibleDC ( hdc );
HBITMAP memBM = ::CreateCompatibleBitmap ( hdc, w, h );
HBITMAP hOld = (HBITMAP)::SelectObject ( memDC, memBM );

tPrintWindow pPrintWindow = 0;
HINSTANCE handle = GetModuleHandle("user32.dll");
if ( handle == 0 )
handle = LoadLibrary("User32.dll");
if ( handle )
pPrintWindow = (tPrintWindow)GetProcAddress(handle, "PrintWindow");
if ( pPrintWindow )
pPrintWindow(hWnd, memDC, 0 );

COLORREF cr = GetPixel(memDC, 10, 10);
if(cr != CLR_INVALID)
{
// make a string
char psz[100];
sprintf(psz, "%x", cr);

// print
MessageBox(NULL, psz, "working", MB_OK);
}

cheers

golanshahar
November 14th, 2005, 12:09 PM
if you want to get couple of pixles its fine. however if you want all pixles better thing to do is use ::GetBitmapBits(..) (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_7gmr.asp) and then work on the raw data.

PS: dont forget to delete objects - the code you posted will cause gdi leaks..
you can look again in the code i posted you before with PrintWindow(..) there is no leakage there ;)

Cheers