Sign in to follow this  

Win32 C++ getting relative mouse movement

This topic is 4018 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

This has been asked a zillion times but all I could find as a solution is: comparing current mouse position with last mouse position and setting the mouse posiition back to its original position, but this is not the way i wanna do it. There has to be a way to get the absolute mouse movement regardless of the cursor on the screen/window. edit: changed title to "relative" [Edited by - D3DXVECTOR3 on November 29, 2006 12:47:25 PM]

Share this post


Link to post
Share on other sites
I believe he means relative mouse movement, basically the one you would like to implement for a first person view.

I think the solution you already mentioned is the most commonly used one. There is one thing you could do though, you could limit the mouse area via ClipCursor(...), otherwise the maximum movement speed per loop would depend on the screen resolution of the user.

Share this post


Link to post
Share on other sites
Im sorry I didnt reply sooner but i was bussy learning for an exam I had today, which i passed btw :D

Yes, i meant relative mouse movement. There has to be a function where you can get this because when you move the mouse it triggers an event and windows knows when the mouse cant move anyt further away (screen bound)

Share this post


Link to post
Share on other sites
If you're looking for absolute screen coordinates there are a few ways to get them. To grab the absolute screen coordinates of the mouse, you might install a mouse hook. Something like this:

MOUSEHOOKSTRUCT gMhs;
HHOOK gHhk(NULL);
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam);

in your init routine ...
{
HRESULT hr;

...

gHhk = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseProc,
NULL, GetCurrentThreadId());
if( NULL == gHhk )
{
hr = HRESULT_FROM_WIN32(GetLastError());
... how ever you handle things ...
}

...
}

in your cleanup routine ...
{
...
if( NULL != gHkh )
(void) UnhookWindowsHookEx(gHhk);
gHhk = NULL;
...
}

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if( nCode >= 0 )
{
gMhs = *(MOUSEHOOKSTRUCT*)lParam;
}
return CallNextHookEx(gHhk, nCode, wParam, lParam);
}

With these snips of code, the members

gMhs.pt.x
gMhs.pt.y

will have the absolute screen coordinates of the mouse. You can use that with window location data to determine the relative location of items within your client rectangle vs the position of the mouse.

For the window you may already be doing something similar, but I'll post that here too.

WINDOWINFO gWindowInfo;
if( 0 == GetWindowInfo(hWnd, &gWindowInfo) ){ ... handle failure ... };

then check,

gWindowInfo.rcWindow
gWindowInfo.rcClient

The window rect will be the outer bounds of your window and the client rect the outer bounds of the client area within that window. Both of these are specified in screen coordinates and adjust as the window is manipulated.

That should give you the beginnings of a set of absolute screen coordinate data to play with, keeping in mind that should the user manipulate the window and/or display things will change.

Regards,
Textile

Share this post


Link to post
Share on other sites
Urgh... textile's solution, while technically feasible, is really not advisable.

Use the APIs that Windows makes available to you, specifically GetCursorPos, and ScreenToClient if you want to translate the coordinates to be relative to a particular window.

In general, before you do anything whatsoever in code, ask yourself, "What are the chances that somewhere, out of the millions of programmers in the world, somebody else has wanted to do this before?" If the chances are greater than zero, look for their solution before reinventing the wheel. Look ten times if your reinvented wheel involves something as hideously bad as a message hook.

Share this post


Link to post
Share on other sites
1st all thanks for all the replies they are all very usefull, but still doesn't solve my problem. the values I'm getting are all between 0~screenWidth and 0~screenHeight. What I wanna know is how much the mouse was moved no matter what, even when the mouse/cursor is at the edges of the screen (monitor).

Share this post


Link to post
Share on other sites

This topic is 4018 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this