2016-10-15 63 views
1

隨着Monogame我能夠切換到使用ToggleFullScreen沒有任何問題,全屏模式,但是當我試圖切換回窗口模式,我得到這樣的:Monogame崩潰ToggleFullScreen後回到窗口

SharpDX.SharpDXException未處理HResult = -2005270527
消息= HRESULT:[0x887A0001],模塊:[SharpDX.DXGI],ApiCode: [DXGI_ERROR_INVALID_CALL/InvalidCall],消息:應用程序發出 調用無效。調用的參數或某個對象的狀態 不正確。啓用D3D調試層以便 通過調試消息查看詳情。

源= SharpDX堆棧跟蹤: 在SharpDX.Result.CheckError() 在SharpDX.DXGI.SwapChain.ResizeBuffers(的Int32 bufferCount,的Int32寬度,高度的Int32,格式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窗體與用戶單擊按鈕。但由於上面的代碼工作我想,也許這只是在更新方法被調用。所以我嘗試這樣做:

private bool shouldToggleFullscreen = false; 

public void ToggleFullScreen() { 
    shouldToggleFullscreen = true; 

} 

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

這是行不通的。我得到了與上面相同的錯誤。

我剛找到答案。這是因爲遊戲不是活動窗口。現在我只需要找出如何給予重點。

+0

兩件事 - 做到這一點 - 「啓用D3D調試層,以便通過調試消息查看細節。」然後檢查「調用的參數或某個對象的狀態不正確。」第一個應該告訴你,它是錯誤的參數還是對象的狀態。 – ChrisF

回答

0

問題是,我試圖切換全屏模式,而窗口沒有集中(爲什麼這是一個問題,我不知道)。

所以在切換之前,我必須確保首先關注遊戲窗口。

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