Getting color of a point on screen?

I'm trying to write a block of code that, when run, will grab the color of the point under the cursor. So far, my approach looks like this...

COLORREF GetColorUnderCursor() {
   HWND hWnd;
   HDC hDC;
   POINT pntCursor;
   COLORREF srColorFound;

   // get the color position
   GetCursorPos(&pntCursor);

   // get the desktop handle
   hWnd = GetDesktopWindow();

   // get the DC of this window
   hDC = GetDC(hWnd);

   //********
   // get the color of the point
   srColorFound = GetPixel(hDC, pntCursor.x, pntCursor.y);
   //********

   // free the DC
   ReleaseDC(hWnd, hDC);

   // return the color
   return srColorFound;
}

But the color returned is always (255,255,255), the invalid result... I'm not that familiar with how to get the color of the point via the DC. Can anyone fill in the blank?
LVL 1
jbabcockAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WDBCommented:
At the position of the cursor, I believe the color will always come back as the color of the cursor. If your cursor is white then it will always be white, if it's black it will always be black etc...
0
DrDelphiCommented:
There are 2 things to consider here.

1) as WDB suggests, your cursor
    position is actually what you see if you look at GetCursorPos(x,y). To see one pixel next to it , either subtract or add one to either of the elements of the point passed to getcursorpos.  


2). GetPixel does not work on all systems... From the help files:

Not all devices support GetPixel. An application should call GetDeviceCaps to determine whether a specified device supports this function.

0
mikeblasCommented:
The problem is that you're not working on the desktop window.

In Windows NT 4 and Windows 95 with the shell integration on, and in Windows 98 and in and Windows 2000, the shell is putting a different window up over the whole desktop. Since the desktop window isn't visible, the GetPixel() call is failing.

I think you'll have better luck if you first find the window under the cursor point and get the colour from that window's DC. Even then, the issue will still come down to being able to party on the DC of a window in another process.

You might have to capture the screen to a memory DC and then find the colour of the point you want.

..B ekiM
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

nietodCommented:
Mike, is there a reason to not just get a DC for the display using CreateDC("DISPLAY",NULL,NULL,NULL)?  That seems the most direct to me.  Is there a disadvantage?
0
mikeblasCommented:
> Mike, is there a reason to not just get a DC for the
 > display using CreateDC("DISPLAY",NULL,NULL,NULL)?  

If the screen DC is what you want, that's how you get it.

..B ekiM
0
nietodCommented:
But isn't that what he wants for this?
0
DaveRidgwayCommented:
Try this code, I use this under a timer event.

=======================
var
   tPt : TPoint;
   dc : THandle;
   wnd : THandle;
   cref : COLORREF;
begin
     if not(GetCursorPos(tPt)) then
        exit;

     wnd := WindowFromPoint(tPt);
     if wnd = 0 then exit;
     Windows.ScreenToClient(wnd, tPt);

     dc := GetWindowDC(wnd);
     if dc = 0 then exit;

     try
        cref := Windows.GetPixel(dc, tPt.X, tPt.Y);
     finally
        ReleaseDC(wnd, dc);
     end;
end;
============================

cref will contain the TColor value of what is under the cursor
0
mikeblasCommented:
Wow; an ancient question with some spurious activity!

 nietod> But isn't that what he wants for this?

Usually not. The screen is usually covered by something else--specifically, the shell window that explorer puts up.

..B ekiM
0
DaveRidgwayCommented:
Well, only commented on it because I was working on the thing yesterday and was looking for tips :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.