Translate Mouse coords into Window coords.

11/02/2015 11:36 Xio.#1
I have a mouse hook that gives me raw location data based on the screen resolution.

If you run a game in windowed mode, the coordinates won't match. If you run a game in fullscreen but you have 2 or more monitors, they won't match either.

If the screens are different size(resolution) they wont match either.


How would I translate the location data into the (game)window's relative location?

Get the Left / Top properties of the window using p/invoke, getting the size of the window using p/invoke and calculating the real size based on the OS's theme and then do some sort of scaling?


[Only registered and activated users can see links. Click Here To Register...]


or simply ask the user to move the mouse to the top left corner to get the 0,0 of the game? What about ego shooters where there is no mouse? You can basically spin around forever since there is no left edge just a upper one.
11/03/2015 13:24 UniveX'#2
You could just get the window rectangle & then moving relative to window, that would be WindowRect.Left + X where X is the coord and for Y it would be WindowRect.Top + Y.

Simple pseudo:
Code:
static public void SetMouse(IntPtr hwnd, Point p)
        {
            RECT rectHandle = new RECT();
            GetWindowRect(hwnd, out rectHandle);
            Cursor.Position = new Point(p.X + rectHandle.Left, p.Y + rectHandle.Top);
        }
GetWindowRect is WinAPI, simply include that & you should be fine. You could also just use PostMessage to solve this. If you have some issue, feel free to hit me up. :)

Have a nice day,
UniveX
11/03/2015 21:07 0ptimist#3
You can get the real size with the GetClientRect function.
With the ClientToScreen function you can do the mapping to screen coordinates.