在 C# XNA 4.0 中使用转义键切换全屏

Toggling fullscreen using the escape key in C# XNA 4.0

现在我正在开发一款平台类型的游戏,但我很难使用转义键退出全屏模式(进入窗口模式)。我一直在互联网上寻找解决方案,但我还没有找到任何对我有帮助的东西。

这是我在游戏中实现的第一件事,所以我还没有太多代码。

构造函数:

     graphics = new GraphicsDeviceManager(this);
        // screen size windowed
        if (IsFullScreenEnabled == false)
        {
            graphics.PreferredBackBufferHeight = 730;
            graphics.PreferredBackBufferWidth = 1000;
        }
        // fullscreen
        if (IsFullScreenEnabled == true)
        {
            graphics.IsFullScreen = true;
        }
        // mouse visible
        IsMouseVisible = true;

这里是转义键的代码:

    if (ks.IsKeyDown(Keys.Escape))
        {
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
        }

非常感谢任何帮助,感谢您花时间阅读这个 and/or 答案。

IsFullScreen 在屏幕设置后无效。

在游戏过程中,您应该使用 GraphicsDeviceManager.ToggleFullScreen

MSDN:

Toggles between full screen and windowed mode. This method has no effect on the Xbox 360.

替换

    graphics.IsFullScreen = false;
    graphics.ApplyChanges();

...与:

    graphics.ToggleFullScreen ();

...正如 Bjarke 提到的那样,您可能希望确保您对上述语句的警卫正确检查先前和当前的键盘状态以确定 是按下和释放的键 而不仅仅是 是向下键 以避免不必要的和重复的屏幕切换。请参考Detecting a Key Press.

MSDN 示例:

    private void UpdateInput()
    {
        KeyboardState newState = Keyboard.GetState();
        // Is the SPACE key down?
        if (newState.IsKeyDown(Keys.Space))
        {
            // If not down last update, key has just been pressed.
            if (!oldState.IsKeyDown(Keys.Space))
            {
                backColor = 
                    new Color(backColor.R, backColor.G, (byte)~backColor.B);
            }
        }
       .
       .
       .
        // Update saved state.
        oldState = newState;
    }

这假设您的视频卡 and/or 驱动程序能够在 window 中显示 3D。

告诉我更多