2013-03-09 60 views
0

注意:這個問題是而不是關於嵌入到另一個!並行運行Winform和XNA遊戲

我在考慮一種方法來製作XNA遊戲窗口stop pausing its execution,它是dragged or resized,因爲它在大多數情況下會中斷網絡連接並導致與遊戲服務器的失步。擁有一個無邊界遊戲窗口和一個可視化容器的winform可以做到這一點。問題是,當用戶調整假遊戲窗口邊界(實際上是winform)時,遊戲窗口會檢查並調整邊界以適應winform的客戶區域。聽起來很簡單,但我一直在麻煩做這項工作。

兩個遊戲窗口和WinForm的應該知道對方的存在,因此,如果重點是在WinForm的,它會立即轉移到遊戲窗口,並在遊戲窗口大小調整自身以適應在WinForm,輪詢大小的變化,或也許等待事件發生。我想這涉及交換窗口句柄。

還有this very recent question,幾個小時前問道如何讓兩個WinForms一起運行。希望它可以幫助你幫助我,從而幫助我們:)


也對這個問題:

XNA How to Render and Update while resizing

XNA Is Running Slow when focus is removed

回答

0

原來這並不難,儘管它可能會帶來不良的副作用,例如從遊戲中竊取資源,導致自發的滯後(稍微顯着),並使其完美運行,但這需要一些時間。

我所做的是創建一個新的Form併爲其ResizeEnd事件分配了一個事件處理程序。事件處理程序將public static Rectangle fakeWindowRect設置爲虛擬窗口客戶區的新矩形,該區域在Form.RectangleToScreen()方法的幫助下計算。

這裏有一些代碼段:

static void Main(string[] args) 
{ 
    System.Windows.Forms.Form f = new System.Windows.Forms.Form(); 
    f.ClientSize = new System.Drawing.Size(800, 600); 
    f.TransparencyKey = f.BackColor; 
    ((Action)(() => System.Windows.Forms.Application.Run(f))).BeginInvoke(
                   null, null); 

    using (Game1 game = new Game1()) 
    { 
     f.ResizeEnd += new EventHandler(game.f_LocationChanged); 
     game.Run(); 
    } 
} 

public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    public static Rectangle windowRect; 

    /* ... */ 

    protected override void Update(GameTime gameTime) 
    { 
     if (windowRect.X != this.Window.ClientBounds.X || 
      windowRect.Y != this.Window.ClientBounds.Y || 
      windowRect.Width != this.Window.ClientBounds.Width || 
      windowRect.Height != this.Window.ClientBounds.Height) 
     { 
      // this method sets the game window size, but not location 
      InitGraphicsMode(windowRect.Width, windowRect.Height, 
       this.graphics.IsFullScreen); 

      var win = System.Windows.Forms.Control.FromHandle(
      this.Window.Handle) as 
       System.Windows.Forms.Form; 

      win.SetBounds(windowRect.X, 
          windowRect.Y, 
          windowRect.Width, 
          windowRect.Height); 
      win.Activate(); 
     } 
    } 


    public void f_LocationChanged(object sender, EventArgs e) 
    { 
     var FakeWindow = sender as System.Windows.Forms.Form; 

     var drawClientArea = FakeWindow.RectangleToScreen(
           FakeWindow.ClientRectangle); 
     windowRect = new Rectangle(
        drawClientArea.X, 
        drawClientArea.Y, 
        drawClientArea.Width, 
        drawClientArea.Height); 
    } 

} 

實現可能是可怕的,錯周圍的一切,但它的作品沒有在偷遊戲中的所有資源,甚至調整假形式的遊戲滴一些幀時,但不會完全停頓。

所以我測試了它,它的工作原理,但它主要是爲了科學,我不打算在短時間內使用這種方法。也許當我真的,真的需要它。