jbabcock
asked on
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?
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?
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...
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Mike, is there a reason to not just get a DC for the display using CreateDC("DISPLAY",NULL,NU LL,NULL)? That seems the most direct to me. Is there a disadvantage?
> Mike, is there a reason to not just get a DC for the
> display using CreateDC("DISPLAY",NULL,NU LL,NULL)?
If the screen DC is what you want, that's how you get it.
..B ekiM
> display using CreateDC("DISPLAY",NULL,NU
If the screen DC is what you want, that's how you get it.
..B ekiM
But isn't that what he wants for this?
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
=======================
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
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
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
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
Well, only commented on it because I was working on the thing yesterday and was looking for tips :)