简体   繁体   English

不启用时关闭鼠标交互

[英]Turn off mouse interactivity when not on applicaiton

Lets say I have my C# program running, there is a button on the screen that if I put my mouse over it, it will change the game to a new page. 可以说我正在运行C#程序,屏幕上有一个按钮,如果将鼠标放在该按钮上,它将把游戏切换到一个新页面。 Now I put another window in front of the project, lets say Google Chrome, and I am searching the web and clicking things, as I do that the project that is running right now is interacting with the mouse still and can have the button pressed even if I can't see the button. 现在,我在项目前面放了另一个窗口,假设是Google Chrome,然后我正在网上搜索并单击内容,因为我正在运行的项目仍在与鼠标交互,甚至可以按下按钮。如果我看不到按钮。 How do I turn this interaction off? 如何关闭此互动?

By using IsActive in your implementation of the Game class (called Game1 by default), you can avoid receiving input while your game is not in focus. 通过在Game类的实现中使用IsActive(默认情况下称为Game1),可以避免在游戏未聚焦时接收输入。

protected override void Update(GameTime gameTime)
{
    base.Update(gameTime);

    if (!IsActive)
    {
        return;
    }

    // ... do regualr update.
}

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.isactive.aspx http://msdn.microsoft.com/zh-CN/library/microsoft.xna.framework.game.isactive.aspx


Alternatively, if you already have some Game State Management system, you can override the OnDeactived method in Game. 或者,如果您已经拥有某些游戏状态管理系统,则可以覆盖游戏中的OnDeactived方法。 This function will get called when the game looses focus. 当游戏失去焦点时,将调用此函数。

protected override void OnDeactivated(object sender, EventArgs args)
{
    base.OnDeactivated(sender, args);

    // If we are in a state which it makes sense to pause the game, then do it now.
    if (GameObjectManager.pInstance.pCurUpdatePass == BehaviourDefinition.Passes.GAME_PLAY)
    {
        GameObjectManager.pInstance.pCurUpdatePass = BehaviourDefinition.Passes.GAME_PLAY_PAUSED;
    }
}

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.ondeactivated.aspx http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.ondeactivated.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM