0

When using mouse controls in XNA (MonoGame, FNA) I use methods like:

    public static bool IsMouseClickedLeft()
    {
        // No clicks if game is not the active application
        if (Game.IsActive == false) return false;

        if (mouseState.LeftButton == ButtonState.Pressed) return true;
        return false;
    }

However, in windowed mode (if not fullscreen) mouse clicks (and mouse movement) is recognized and processed even if the game window is covered or partly covered by other windows e.g. a browser or the Windows file explorer. How can I make sure that only mouse clicks are recognized that click on visible (not covered) parts of the game window.

ares_games
  • 1,019
  • 2
  • 15
  • 32

1 Answers1

1
if (ms.LeftButton == ButtonState.Pressed
&& _previousMouseState.LeftButton == ButtonState.Released
&& this.IsActive
&& ms.X >= 0 && ms.X < graphics.PreferredBackBufferWidth
&& ms.Y >= 0 && ms.Y < graphics.PreferredBackBufferHeight
&& System.Windows.Forms.Form.ActiveForm != null
&& System.Windows.Forms.Form.ActiveForm.Text.Equals(this.Window.Title)) {

//Mouse was clicked and the form is active }

This was found at http://www.systemroot.ca/2011/08/xna-check-if-a-click-is-on-the-game-form-window/

Essentially you are checking if the mouse is within the screen. The gentleman who wrote that post was also using a custom map editor and found he needed to check for active forms as well, which is what the last two lines are doing, ensuring that the active window is indeed the game window.

  • Unfortunately, there seems to be no 'System.Windows...' in recent .net versions? – ares_games May 01 '16 at 15:03
  • 1
    Right click on the project in Solution Tree, Add Reference, Select System.Windows.Forms. You need to add reference to some non-default assemblies sometimes. Taken from this question: http://stackoverflow.com/questions/6639468/c-forms-does-not-exist-in-the-namespace-system-windows/6639482 – Slubberdegullion May 01 '16 at 21:38