简体   繁体   English

[C#] [XNA 3.1]如何在一个Windows窗体中托管两个不同的XNA窗口?

[英][C#][XNA 3.1] How can I host two different XNA windows inside one Windows Form?

I am making a Map Editor for a 2D tile-based game. 我正在为2D基于图块的游戏制作地图编辑器。 I would like to host two XNA controls inside the Windows Form - the first to render the map; 我想在Windows窗体中托管两个XNA控件 - 第一个渲染地图; the second to render the tileset. 第二个渲染tileset。 I used the code here to make the XNA control host inside the Windows Form. 我在这里使用代码 Windows窗体中创建XNA控件主机。 This all works very well - as long as there is only one XNA control inside the Windows Form. 这一切都很好 - 只要Windows窗体中只有一个XNA控件。 But I need two - one for the map; 但我需要两个 - 一个用于地图; the second for the tileset. tileset的第二个。 How can I run two XNA controls inside the Windows Form? 如何在Windows窗体中运行两个XNA控件? While googling, I came across the terms "swap chain" and "multiple viewports", but I can't understand them and would appreciate support. 谷歌搜索时,我遇到了“交换链”和“多个视口”这两个术语,但我无法理解它们并希望得到支持。

Just as a side note, I know the XNA control example was designed so that even if you ran 100 XNA controls, they would all share the same GraphicsDevice - essentially, all 100 XNA controls would share the same screen. 正如旁注,我知道XNA控件示例的设计使得即使您运行100个XNA控件,它们也将共享相同的GraphicsDevice - 实质上,所有100个XNA控件都将共享同一个屏幕。 I tried modifying the code to instantiate a new GraphicsDevice for each XNA control, but the rest of the code doesn't work. 我尝试修改代码以为每个XNA控件实例化一个新的GraphicsDevice,但其余的代码不起作用。 The code is a bit long to post, so I won't post it unless someone needs it to be able to help me. 代码有点长,所以我不会发布它,除非有人需要它能够帮助我。

Thanks in advance. 提前致谢。

I have done something similar to what you are trying to do. 我做了类似于你想要做的事情。 All you need to do is tell the graphics device where to present the "stuff" you have rendered. 您需要做的就是告诉图形设备在哪里展示您渲染的“东西”。 You do this by passing it a pointer to a canvas. 你通过传递一个指向画布的指针来做到这一点。

Here is a sample form class: 这是一个示例表单类:

public class DisplayForm : Form
{

    IntPtr canvas;
    Panel displaypanel;

    public Panel DisplayPanel
    {
        get { return displaypanel; }
        set { displaypanel = value; }
    }

    public IntPtr Canvas
    {
        get { return canvas; }
        set { canvas = value; }
    }

    public DisplayForm()
    {
        displaypanel = new Panel();
        displaypanel.Dock = DockStyle.Fill;

        this.canvas = displaypanel.Handle;
        this.Controls.Add(displaypanel);
    }

}

Then simply add this to your game class draw call: 然后只需将其添加到您的游戏类绘制调用中:

graphics.GraphicsDevice.Present(displayform.Canvas);

After you are done drawing to that instance of DisplayForm you can clear, render something else, and call Present again pointing to another canvas. 完成绘制到DisplayForm实例后,您可以清除,渲染其他内容,再次调用Present指向另一个画布。

Just a thought but have you considered making this app of yours an MDI app? 只是一个想法,但你考虑过让你的这个应用程序成为MDI应用程序吗?

that way you can load a form that contains 1 instance of xna multiple times. 这样你就可以多次加载一个包含xna实例的表单。

Failing that ... do what RodYan suggests :) 失败了...做RodYan建议:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM