【发布时间】:2013-01-26 11:35:01
【问题描述】:
我在 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)”函数中的注释,我将不胜感激。
提前谢谢
【问题讨论】:
-
你为什么在 XNA 中使用 winforms?我相信如果将两者混合在一起,您会看到这是典型的结果,因为绘图缓冲区不能很好地配合使用。我建议使用更像xnaml.codeplex.com
-
@sec_goat 实际上你已经把它弄反了。 XNA 自己的内部实现使用 WinForms - WinForms 上的正确实现不会受到性能损失。 WPF 需要复杂的缓冲区设置以避免严重的性能损失。
标签: c# winforms xna-4.0 performance