ToggleFullScreen返回窗口

时间:2016-10-15 16:23:42

标签: c# xna monogame sharpdx

使用Monogame我可以使用ToggleFullScreen切换到全屏模式,没有任何问题,但当我尝试切换回窗口模式时,我得到了这个:

  

SharpDX.SharpDXException未处理HResult = -2005270527
  消息= HRESULT:[0x887A0001],模块:[SharpDX.DXGI],ApiCode:   [DXGI_ERROR_INVALID_CALL / InvalidCall],消息:应用程序发了一个   呼叫无效。呼叫的参数或状态   一些对象是不正确的。启用D3D调试层以便   通过调试消息查看详细信息。

     

Source = SharpDX StackTrace:          在SharpDX.Result.CheckError()          在SharpDX.DXGI.SwapChain.ResizeBuffers(Int32 bufferCount,Int32 width,Int32 height,Format newFormat,SwapChainFlags)   swapChainFlags)          在Microsoft.Xna.Framework.Graphics.GraphicsDevice.CreateSizeDependentResources(布尔值   useFullscreenParameter)          在Microsoft.Xna.Framework.GraphicsDeviceManager.ApplyChanges()          在Microsoft.Xna.Framework.GraphicsDeviceManager.ToggleFullScreen()

这里有一些代码:

private GraphicsDeviceManager graphics;
private Vector2 baseScreenSize = new Vector2(1280, 720);

public Game1() {
   graphics = new GraphicsDeviceManager(this);
}

protected override void Initialize() {
   base.Initialize();
   GraphicsDevice.SamplerStates[0] = SamplerState.PointWrap;

   //Work out how much we need to scale our graphics to fill the screen
   float horScaling = GraphicsDevice.PresentationParameters.BackBufferWidth / baseScreenSize.X;
   float verScaling = GraphicsDevice.PresentationParameters.BackBufferHeight / baseScreenSize.Y;
   Vector3 screenScalingFactor = new Vector3(horScaling, verScaling, 1);
   globalTransformation = Matrix.CreateScale(screenScalingFactor);
}

public void ToggleFullScreen() {
   graphics.ToggleFullScreen();

}

正在从表单上的按钮调用ToggleFullScreen方法。

该异常是什么意思,我该如何解决?

编辑:

进一步的测试显示了...... 当按下F键时,我在更新方法中设置了一个开关:

protected override void Update(GameTime gameTime) {
            if (Keyboard.GetState().IsKeyDown(Keys.F)) {
                if (!debounce) { graphics.ToggleFullScreen(); debounce = true; }
            }
            else
                debounce = false;
}

这有效!我可以切换到全屏模式。

但我希望能够在用户点击按钮的情况下从Windows窗体切换模式。但是由于上面的代码工作,我想也许这只需要在Update方法中调用..所以我尝试了这个:

private bool shouldToggleFullscreen = false;

public void ToggleFullScreen() {
   shouldToggleFullscreen = true;

}

protected override void Update(GameTime gameTime) {
   if(shouldToggleFullscreen) {
      graphics.ToggleFullScreen();
      shouldToggleFullscreen = false;
   }
}

这不起作用。我得到与上面相同的错误。

我刚刚找到答案。这是因为游戏不是活动窗口。现在我只需要找出如何重点关注。

1 个答案:

答案 0 :(得分:0)

问题是我试图在窗口未聚焦时切换全屏模式(为什么这是一个我不确定的问题)。

所以在我切换之前,我必须确保首先关注游戏窗口。

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
...
if(shouldToggleFullscreen) {
   SetForegroundWindow(Window.Handle);
   if (IsActive) {
      graphics.ToggleFullScreen();
      shouldToggleFullscreen = false;
   }
}