0

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) " 関数のコメントを見てくれたら本当に感謝しています。

thx事前に

4

1 に答える 1

2

専門家のShawnHargreavesから入手してくださいhttp://blogs.msdn.com/b/shawnhar/archive/2007/01/23/using-xna-with-winforms.aspx

たとえば、WinFormsアプリケーション内でXNAゲームをホストするために、XNAゲームクラスを通常とは異なる場所で使用しようとしている人をよく見かけます。

それは通常悪い考えです。

Gameクラスはシンプルに設計されており、コーディングを開始できるようにすべてが自動的に設定されます。複雑なことをしていて、ウィンドウの作成方法の詳細をより細かく制御したい場合、これは邪魔になるだけです。

Microsoftは、WinFormsで使用するXNAグラフィックスデバイスをセットアップするためのコードサンプルをここここに提供しています。

于 2013-01-26T12:21:30.863 に答える