Win32 C++ getting relative mouse movement

Started by
10 comments, last by anist 17 years, 4 months ago
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]
Advertisement
I'm not sure what you mean by "absolute mouse movement" but maybe you're looking for WM_INPUT?
Handle mouse messages for the HWND returned by GetDesktopWindow(). It's the topmost window (ie, the whole screen).
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
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.
My Blog
SetMousePos()?
Or do you mean like 'ghost mouse'?
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)

If you are using directx with direct input you get relative movement from mouse.
Like x=-2 y=-3, and if you dont move mouse x,y = 0.
Something like this?
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
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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).

This topic is closed to new replies.

Advertisement