Jump to content
  • Advertisement
Sign in to follow this  
RogerThat123

C++ Mouse Position with GetPixel?

This topic is 3252 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

sq6wxc.jpg http://i36.tinypic.com/sq6wxc.jpg I need to use GetPixel(hdc, mouse.x, mouse.y) to get my pixel color. However in the pic you can see that GetPixel will always return the middle of my window (this is supposed to happen). When the mouse is moved, i capture the movement, and change my mouse position accordingly, and then the mouse position will be reset to the middle again (not mine though). The problem as you can see is that when I try to get the pixel color from (0,0) it will return a red color (ex in pic) and not the black that is drawn on that pixel. There must be some trick i can do to fix it.
HDC TargethDC;
	HWND TargetHwnd;
	DWORD ColorR;
	
	TargetHwnd = GetForegroundWindow();
	TargethDC = GetWindowDC(TargetHwnd);

	tagPOINT tp;
	tp.x = gMouse.x; //gmouse is my mouse pos
	tp.y = gMouse.y;

	ColorR = GetPixel(TargethDC, tp.x, tp.y);

	ReleaseDC(TargetHwnd,TargethDC);

	r = GetRValue(ColorR);
	g = GetGValue(ColorR);
	b = GetBValue(ColorR);
[Edited by - RogerThat123 on October 25, 2009 6:24:04 PM]

Share this post


Link to post
Share on other sites
Advertisement
i dont understand why GetPixel(hdc,mousex,mousey)

for 0,0 returns the color of the title bar, it makes no sense. Why would anyone want to read those pixels.

If you specified the hdc it should read within the actual window.

Share this post


Link to post
Share on other sites
GetCursorPos() returns the mouse position in screen coordinates, not window coordinates.

Try:
GetCursorPos(&gMouse);
ScreenToClient(yourHwnd,&gMouse);

Share this post


Link to post
Share on other sites
I know, but you have to look @ my pic to understand.

Doesnt matter what I do it will always be off.

GetCursorPos will be the center of my window everytime. So when I ScreenToClient it is still in the center.

But heres how IM getting my screen coordinates if your interested..



tagPOINT tp;
GetCursorPos(&tp);

// It works if you don't use this but fucks up the mouse if your in windowed mode
LPPOINT clientPos = &tp;
ScreenToClient(GetForegroundWindow(), clientPos);

tp.x = clientPos->x;
tp.y = clientPos->y;

if(!pEngine->IsInGame())
{
x = tp.x;
y = tp.y;
}
else
{
if(tp.x - HALFX != 0
|| tp.y - HALFY != 0)
{
x += tp.x - HALFX;
y += tp.y - HALFY;
}

if(x > FULLX)
x = FULLX;
else if(x < 0)
x = 0;

if(y > FULLY)
y = FULLY;
else if(y < 0)
y = 0;
}


FULLX is my window width, FULLY the window height.

When you move the mouse, the program resets the mouse to the center (I cant control it)

so i capture the mouse position inbetween moves, so it will add 1 or 2 pixels when i move the mouse...



Share this post


Link to post
Share on other sites
Quote:
When the mouse is moved, i capture the movement, and change my mouse position accordingly, and then the mouse position will be reset to the middle again (not mine though)...
When you move the mouse, the program resets the mouse to the center (I cant control it)
GetPixel will always return the middle of my window (this is supposed to happen).

Why can't you control it? Are you trying to get the pixel color in the window of another application? Can you explain a little better?

Share this post


Link to post
Share on other sites
You won't be able to click the mouse to get the pixel color, but, in your own application when the mouse is not locked by the other application, you can use a keypress to get the color under the current mouse position with something like:

HDC wdc = GetWindowDC(NULL); // desktop window, entire screen
POINT pt;
GetCursorPos(&pt); // get screen coordinates
COLORREF clr = GetPixel(wdc,pt.x,pt.y);
ReleaseDC(NULL,wdc);

Share this post


Link to post
Share on other sites
You dont understand, sorry.

GetCursorPos as u see in the pic, will ALWAYS ALWAYS ALWAYS return the center of MY window.

I posted how I calculated MY x/y value for the mouse, using the locked mouse.

It resets to the middle after I grab my movements so I have my mouse.

I have the mouse coordinate from (0,0) not including the title bar. Basically what I need to solve this is to determine the height of the title bar, and the width of the left edge.

GetClientRect returns the width and height only inside the window, and doesnt include the outer edge.

I need to somehow use GETPIXEL() to determine the color of (0,0) inside my application when 0,0 would be passed into get pixel

Share this post


Link to post
Share on other sites

HDC TargethDC;
HWND TargetHwnd;
DWORD ColorR;

TargetHwnd = GetForegroundWindow();
TargethDC = GetWindowDC(NULL);

POINT lp;

lp.y = gMouse.y;
lp.x = gMouse.x;

ClientToScreen(TargetHwnd, &lp);
ColorR = GetPixel(TargethDC, lp.x, lp.y);
ReleaseDC(TargetHwnd,TargethDC);

r = GetRValue(ColorR);
g = GetGValue(ColorR);
b = GetBValue(ColorR);


If I do like this, I think the positioning is more accurate, this is how you just posted.

But the color is like flickering between the real color, white, and black. (this part is only run when im clicking the mouse) If i click and hold it stays white, if i hold and drag the mouse it shows the real color but if u stop or move too slowly it flickers between the 3 mentioned above. Any reason why this might happen for the code above?

BTW ty for your help

Share this post


Link to post
Share on other sites
You're still not making much sense as to what you're trying to achieve, at least to me :)

Quote:
Original post by RogerThat123
I need to somehow use GETPIXEL() to determine the color of (0,0) inside my application when 0,0 would be passed into get pixel


If you want to get the pixel colour of your window as specified by the top-left arrow in your first diagram, do this:


HDC hDC = GetDC(hWnd);
COLORREF cr = GetPixel(hDC, 0, 0);
ReleaseDC(hWnd, hDC);


Note the use of GetDC instead of GetWindowDC, this returns a DC for just the client area of your application (the white part of your application in your diagram), instead of a DC for the entire application window.

If you instead wanted the colour for where your cursor is, you can use GetCursorPos, then use ScreenToClient, and feed that point to GetPixel with a DC from GetDC like above.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!