我在 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) ”函数中的注释,我将不胜感激。
提前谢谢