Mouse state position when deactivated

Hi all,

While a drag action is performing and the Windows window minimize, mouse position goes wild, giving strange results.

With the command Mouse.GetState().Position, when game is active I correctly get {X:186 Y:326}, otherwise when deactivated I get {X:32473 Y:32507}.

It is something related to how Monogame handles the mouse position, or is it a Windows magic number?

Thank you all

Ciao

Hi @Probe9, Welcome to the Community!

Are you using more than one display?

Try using a single display if so and see if the issue persists.

This page may help with other factors.

Window Coordinate System - Win32 apps | Microsoft Docs

Happy Coding!

Those numbers look like the result of integer overflow to me. The number 32,767 is the highest positive value of a signed 16-bit integer, which is probably how MonoGame internally stores the mouse position.

Since the mouse position reported by MonoGame is dependent on the upper-left corner of the render area of the window, it’s probably not really possible to report meaningful mouse locations when the window is minimized.

To handle this, you can just pause/disable your game when the window is minimized. You can do this by subscribing to the Window.ClientSizeChanged event. Alternately, you could subscribe to the Deactivated event of the Game class and pause when that occurs, though only if you want to pause the game whenever it loses focus and not just when it’s minimized.

3 Likes

I have tried a single display but the issue still persists. Kindly help me out.

@MrValentine I have 2 monitors and one Monogame GameWindow, the window is running only in the main screen an minimize into it.

From the microsoft doc, about the MapWindowPoins function, they write:

If the function fails, the return value is zero.

I tried it in a clean WinForm application, and the strange values are coming out from the MapWindowPoins function, but with a fixed {X=32000,Y=32000}.
I found here (Where did windows minimize to before the taskbar was invented? - The Old New Thing) that for historical reasons Windows moves, minimized windows, to this strange coordinates, to avoid screen overlaps.

@HopefulToad Monogame handle the point structure with int32 values, and the MapWindowPoins function uses c++ int, so should be fine with it.

I solved it by following your suggestion.

  1. I store the last valid position, and if the game is IsActive = false, I give back the last valid position, ignoring the current Monogame mouse position, so when the game maximize the user find everything as before.
  2. To be extra safe, I clamp the mouse values inside the window bound.

Thank you for the replies.

Ciao

2 Likes

I had wondered if it might instead be Windows moving the location of the window to some far-off coordinates when a window is minimized.

Umm, did you just hijack this thread or something?

@MrValentine Why would I do that? Sorry I am completely new here and do not know much.

Therefore, not a clue why you were asking me or what you were asking about, this is @Probe9’s thread, so, what were you enquiring about? if it is not related to this thread, please start a new topic, and ask there.

EDIT

If it relates to this topic, please let it flow its course and follow along, that was rather confusing for me.