WinForm中的嵌入式XNA 4.0游戏运行速度太慢,我该怎么办?

时间:2013-01-26 11:35:02

标签: c# winforms xna-4.0 performance

我在WinForm中有一个嵌入式XNA 4.0游戏(用于关卡编辑器)。代码采用以下格式:

在游戏课程中:

protected override void Initialize(){
    //initialization logic here  
    base.Initialize();

    SysWinForms.Form gameWindowForm(SysWinForms.Form)SysWinForms.Form.FromHandle(this.Window.Handle);
    gameWindowForm.Shown += new EventHandler(gameWindowForm_Shown);

    MYFORM = new Form1();
    MYFORM.HandleDestroyed += new EventHandler(myForm_HandleDestroyed);
    MYFORM.Show();
}


void myForm_HandleDestroyed(object sender, EventArgs e)
{
    this.Exit();
}

void gameWindowForm_Shown(object sender, EventArgs e)
{
     ((SysWinForms.Form)sender).Hide(); 
     //this line is important. When this line is commented the XNA + winForm windows are both shown. Also, the xna game is running in the winForm and it is running with modest speed. 
     //but when the line is not commented, than only the winForm window is shown and the xna game is shown inside it, but it is running with 0.5 frames/seconds
}

//The loadContent, unloadContent, update, and game constructor classes remain the same

protected override void Draw(GameTime gameTime)
{
     //draw logic here
     base.Draw(gameTime);
        //this is the actual trick that makes it all happen
        this.GraphicsDevice.Present(new Rectangle(controls.panel1.Location.X, controls.panel1.Location.Y, desired_Width, desired_Height), null, this.MYFORM.PanelHandle);
}

在MYFORM课程中:

public IntPtr PanelHandle
{
    get
    {
        return this.panel1.IsHandleCreated ? this.panel1.Handle : IntPtr.Zero;
    }
}

还有一个自动生成的:

public System.Windows.Forms.Panel panel1;

如果有人看一下代码,特别是“void gameWindowForm_Shown(object sender,EventArgs e)”函数中的注释,我将非常感激。

事先提前

1 个答案:

答案 0 :(得分:2)

从专家Shawn Hargreaves http://blogs.msdn.com/b/shawnhar/archive/2007/01/23/using-xna-with-winforms.aspx

获取
  

经常看到有人试图在不寻常的地方使用XNA Game类,   例如,在WinForms应用程序中托管XNA游戏。

     

这通常是一个坏主意。

     

Game类设计简单,自动设置   一切都准备好开始编码了。如果你在做   一些复杂的东西,想要更多地控制细节   你的窗口已经创建,这只会妨碍你。

Microsoft提供了代码示例herehere,用于设置要与WinForms一起使用的XNA图形设备。