[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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?
0
jbabcock
Asked:
jbabcock
  • 3
  • 2
  • 2
  • +2
1 Solution
 
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now