简体   繁体   中英

XNA with Windows Forms - Opening new window

Okay, so here we go. I'm attempting to make an application, using XNA as the base because of its renderer. One of the things that is necessary in this project is to open a new window (as a dialog), in which is embedded a separate XNA render panel. I'm using this as an interactive preview panel, so I absolutely need XNA to render in there. However, it seems XNA is not very well equipped to do this. I have tried various things myself, but to no avail (either producing errors and not rendering correctly, or rendering in the wrong aspect ratio, etc.). Normally, I would post code here, but since I have had such little luck, there isn't much to post.

My application currently consists of an XNA application embedded within a Form, and I have a button to open the preview panel, which in theory should pop up as a Form Dialog, containing the XNA renderer, to allow me to draw the preview. I have been trying this for several hours, and got nowhere, so I'm asking for a bit of help here.

Thanks, anyway.

EDIT: Okay, I've made a little progress, but I have 2 problems. Firstly, any textures drawn with a sprite batch appear the right dimensions, but filled with solid black. Also, when I open the dialog, and then close it, and close the application, I get an AccessViolationException. I strongly suspect the two errors are linked in some way.

Here is my code initialising the preview dialog. (a is a custom class that essentially consists of a LinkedList of Texture2D objects).

 animPrev = new AnimationPreview(a);
 animPrev.Show();

My AnimationPreview class is an extension of the Form class, and contains a PreviewControl object, which is an extension of the GraphicsDeviceControl found in the XNA Winforms sample. Note that my main form extends the XNA Game class, for various reasons.

The PreviewControl object is set up like this:

protected override void Initialize()
{
    sb = new SpriteBatch(GraphicsDevice);
    Application.Idle += delegate { Invalidate(); };
}

And the Draw method contains:

protected override void Draw()
{
    GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.Violet);
    if (frame != null)
    {
        sb.Begin();
        sb.Draw(Image, Vector2.Zero, Color.White);
        sb.End();
    }
 }

This clears the background of the form violet, as expected, and draws a black box of the same size as Image. This is not expected. Hopefully someone can help me out here.

NOTE: An acceptable alternative would be to convert XNA Texture2D objects to System.Drawing.Image objects. However, I am using XNA 3.1, so I can't just save the texture to a stream and reload it. Actually, after having tried this, it's a bit dodgy, and very slow, so I'd rather not do it this way.

Did you take a look at the following official tutorials / samples?

They should explain everything in my opinion. You even find downloadable source for the samples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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