Advertisement

Win32 Mouse Coordinates wrong

Started by January 24, 2012 08:49 PM
6 comments, last by 21st Century Moose 13 years, 1 month ago
hi ,

im using win32 to get mouse events and coordinates .. this is my code :




LRESULT CALLBACK MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

switch( msg )
{

case WM_LBUTTONDOWN:

int x = LOWORD( lParam ) ;
int y = HIWORD( lParam ) ;

app.setTarget( x, y );

return 0;

};

}



but for some reason the x and y returned from the lParam are wrong ..

thx for any reply smile.png
How is it wrong?

Try using GET_X_LPARAM and GET_Y_LPARAM macros instead of LOWORD and HIWORD. The former return a signed int while the latter return a WORD (unsigned short).
Advertisement
i did this :



case WM_LBUTTONDOWN:

/*
int x = LOWORD(lParam);
int y = HIWORD(lParam);
*/

int x = GET_X_LPARAM( lParam );
int y = GET_Y_LPARAM( lParam );

app.setTarget( x, y );

return 0;


but the values are still wrong :P
If you only want the coordinates, you could try the following (from NeHe's gluUnproject tutorial):


[font=Ubuntu, Arial, sans-serif]

POINT mouse; // Stores The X And Y Coords For The Current Mouse Position[/font]
[font=Ubuntu, Arial, sans-serif]

GetCursorPos(&mouse); // Gets The Current Cursor Coordinates (Mouse Coordinates)[/font]
[font=Ubuntu, Arial, sans-serif]

ScreenToClient(hWnd, &mouse);[/font]

i tried all these solutions but none worked lol,

to explain the problem better :
i created a sprite and positioned it at (250;250) pixels. If i click on the left-top edge of the sprite
instead of 250;250 the mouse gives me 244;230.. so the coordinates are for some reason
messed up..
If you position the sprite at (0, 0), do you get what you expect? Or is the sprite cut/cropped in some way?
Advertisement
The X and Y coordinates you get from the lParam of WM_LBUTTONDOWN are in client coodinates, meaning relative to the client area of the window. That code snippet involving GetCursorPos that someone posted above will also end up in client coordinates. Either you don't want client coordinates or you have a bug somewhere else. Anyway, to say more, would need to know what app.setTarget() does. Is it maybe expecting coodinates that are relative to the window rect or to the screen?
Can you define "wrong"? What values are you getting? What values are you expecting to get? Remember that some of the Windows API will give you coords relative to the screen and some will give you coords relative to the window (which may mean the full window rect or the client rect) - it's your responsibility to know which coords you're getting and to translate from one to the other (in the case of WM_LBUTTONDOWN the coords are relative to the upper-left corner of the window's client area).

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement