MonoGame Mouse.GetState() not getting position

Hi All,

I am relatively new to MonoGame but I player around with XNA quite a lot back in the day.

I am trying to port an old project of mine. This project was a 2D game engine (nowhere near complete) that included a windows forms based tile map editor.

The issue that I am currently having is when using Monogame Game class embedded within a windows form.

In order to achieve this, I am setting the draw surface to a PictureBox as follows:

void graphics_PreparingDeviceSettings(object sender,
PreparingDeviceSettingsEventArgs e)

e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = drawSurface;


where drawSurface is a PictureBox’s IntPtr handle.

The issue comes in when I am trying to get the MouseState in the Update loop.

MouseState ms = Mouse.GetState();

I have also tried MouseState ms = Mouse.GetState(this.Window);

I am unsure why but ms.X and ms.Y are never updated.

Any help would be greatly appreciated.

P.S. This project was created using the Windows monogame project type.

This is an interesting one.

Thinking about it what is probably happening is the windows form is consuming all mouse events so Monogame never sees them.

I would have a look at MonoGame.Forms - Create your Editor Environment! and see if you can use that first.

If not I think you may have to find a way of sending mouse events from the main form to Monogame.

WinFormsGameForm does have a windows message pump so it should be possible.

1 Like

It seems you are creating a windows project. Try connecting the control handle like this:

Microsoft.Xna.Framework.Input.Mouse.WindowHandle = Handle;

Further if you need keyboard input (Keyboard.GetState()) you could use this nice little trick:

private void SetKeyboardInput(bool enable)
    var keyboardType = typeof(Microsoft.Xna.Framework.Input.Keyboard);
    var methodInfo = keyboardType.GetMethod("SetActive", System.Reflection.BindingFlags.NonPublic |     System.Reflection.BindingFlags.Static);
    methodInfo.Invoke(null, new object[] { enable });

And use it like this:


This is how we are doing it in the MonoGame.Forms library.

Hope this helps.

Hi StainlessTobii,

Thank you very much for the reply. I will take a look at the article that you provided as soon as I have the free time available again.

I did manage to find a way to work around the problem and I will post them below in case anyone has the same problem and would like a “quick fix”.

Thanks again!

Hi sqrMin1,

Thank you very much for the reply. I will give this a try as soon as I have the time again.

I did already try setting the WindowHandle, to no avail however. I am unsure if there is perhaps something I did incorrectly there.

I will be posting my temporary fix below, in case anyone runs into a similar problem and I will be trying all solutions that are posted.

Thanks a lot for the response.

Hi All,

The work around that I mentioned in my above replies is as follows:

Create an InputManager class of your own, with properties such as IsMouseLeftClicked, IsMouseRightClicked, etc.

In your Game class, create a public variable to store an instance of this class and instantiate it within the constructor.

Once you have done this, bind mouse events to your render canvas (in my case, a picture box) and allow these to update the Game’s InputManager class via set methods.

In the Game’s Update(), check these properties instead of Mouse.GetState().

I hope this helps somebody with the same issue as me, sorry that the explanation is vague but I am working from memory. Please let me know if you would like a code sample.

Thanks a lot to all who replied again, I will still try the suggested methods as soon as I have a chance and post the results here.