Running on Empty

The few things I know, I like to share.

XNA Framework GameEngine Development. (Part 15, Adding WinForm Support, step 1 to World Builder)


Welcome to Part15 of the XNA Framework GameEngine Development series.  This article will introduce using XNA 2.0 in the WinForms environment.  This is the first obvious step to creating a World Builder application.  I know the XNA team site has an example of how to do this, but I simply did not like how truely difficult it was to get an XNA Game into WinForms using their example. 
So I turned again to the open source community and was not disappointed.  Pedro Güida was kind enough to show an example of doing exactly what I want to do on the codeproject resource site.  This article is my implementation of his work, with a few minor tweeks.



Here is the sourcecode, if you like this series or have suggestions please leave a comment.  Your feedback is very important and is fuel for new ideas.

Simple steps to WinForms

  • Create a Game Project (DONE) – this is what we have been doing all along.
  • Create a Windows Form in our Engine project.
  • Avoid using any references to Windows Forms on the XBOX360.
  • Hide the game window – use the window form handle as the presentation device handle.

Adding a WinForm

Add a new winform to the RoeEngine2 project.  Add a panel to the project window and add a few properties for use later.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RoeEngine2
    public partial class Form1 : Form
        public Form1()

        public Panel XNAPanel
            get { return panel1; }

        public int PanelWidth
            get { return panel1.Width; }

        public int PanelHeight
            get { return panel1.Height; }

        public IntPtr PanelHandle
                return this.panel1.IsHandleCreated ? this.panel1.Handle : IntPtr.Zero;

Updates to base engine

In the RoeEngine class we need to do some work.  Our main goal here is to hide the game form, handle window resize, window close, and Show the WinForm.  Then copy everything we would have drawn in the game window to the new WinForm window.

In the Initialize method add the following code.

#if !XBOX360
            if (_useWinForm)
                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.Resize += new EventHandler(myForm_Resize);
                myForm.XNAPanel.Resize += new EventHandler(XNAPanel_Resize);

Wire up some event handlers for resizing and destroying windows.

#if !XBOX360
        void XNAPanel_Resize(object sender, EventArgs e)
            ApplyResolutionChange(((SysWinForms.Panel)sender).Width, ((SysWinForms.Panel)sender).Height);

        void myForm_Resize(object sender, EventArgs e)
            ApplyResolutionChange(((Form1)sender).PanelWidth, ((Form1)sender).PanelHeight);

        void myForm_HandleDestroyed(object sender, EventArgs e)

        void gameWindowForm_Shown(object sender, EventArgs e)

Finally, show something to the user.  Add the following code to the Draw method.

#if !XBOX360
            if (_useWinForm)

Yes, it is that easy, I do not quite understand why the XNA team site went through so much trouble in their sample.  Could be my inexperience, I would like to discuss possible changes to this solution if some smart folks have anything to offer.  For now, this seems simple enough for what we need to do.

World Builder project

For completeness, I like to have a seperate project that will be my world builder startup project.  This is pretty much the exact same thing as the “normal” game project.  Just this time, we pass true as the useWinForms parameter when creating the game.

                using (EngineManager game = new EngineManager(true))
                    EngineManager.Game = game;


In this article I introduced using XNA in a WinForm project.  This will be the beginning of our World Builder project and gives us a reasonably useful way to easily manipulate the engine.

I look forward to your suggestions and feedback.  I know the XNA team site does this in a very different way from what I am doing.  I would very much like to hear your thoughts and opinions about this solution.


February 29, 2008 - Posted by | C#, XNA


  1. Great series! (and thanks for mention me!)

    Comment by Pete | February 29, 2008 | Reply

  2. I know motivation sometimes becomes a problem, so I’ll just tell you that you’re doing a good job and keep on going. :^)

    Comment by JeBuS | March 2, 2008 | Reply

  3. Pete,
    Of course, credit should always be given when it is due. Your implementation of XNA in WinForms is simple and elegant. I am happy to have the oppurtunity to reproduce your work here.

    Comment by roecode | March 2, 2008 | Reply

  4. JeBuS,
    Thank you, you are correct it is sometimes difficult to stay motivated to complete a project. For me, this is has become a hobby. I don’t see a point in the near future that I would say this series is finished.

    Comment by roecode | March 2, 2008 | Reply

  5. Really nice tutorials, only just found them (you should advertise more :p) and going to go through every single one.. eventually 🙂 Thanks for taking the time out to do these, appreciate it 🙂

    Comment by Graze | March 4, 2008 | Reply

  6. Graze,
    Thank you for your kind comments. I am not much for advertisment, I leave it up to readers like yourself to spread the word. Plus, I simply wouldn’t know where to start spreading the word.

    Thank you for your interest.

    Comment by roecode | March 4, 2008 | Reply

  7. Keep up the good work!

    Comment by Pete | March 5, 2008 | Reply

  8. Pete,
    Will Do!

    Comment by roecode | March 5, 2008 | Reply

  9. BTW, you’re now on my blog’s list of links 😉

    Comment by Pete | March 5, 2008 | Reply

  10. Thank you,
    I need feedback from peers in all areas of development. I’m certain I will get some helpful suggestions from your redirection.

    Comment by roecode | March 5, 2008 | Reply

  11. How’s things progressing? Waiting eagerly for part 20 of this excellent series! 🙂

    Comment by Zenox | March 26, 2008 | Reply

  12. hello,

    Your tutorial series are great, I just discover them by doing a research. I guess you should update your progress on ziggyware.

    Anyway I would like to see more of the world builder.For example how to save the level change. Storing object data and reuse it. I have no idea how to do something like this.

    Anyway keep it up

    Comment by black reaper | December 24, 2008 | Reply

    • Thank you for your suggestions. You probably know this already, but I do not advertise my blog on any other sites. I leave it up to readers and word of mouth to advertise articles they enjoy.

      I will definitly spend some time on the world builder in upcoming articles.

      Comment by roecode | January 6, 2009 | Reply

  13. […] I found a site that explained it all much better, and in a way that was much easier to understand for a nub like […]

    Pingback by Praxis » Blog Archive » Collisions & Winforms | June 20, 2009 | Reply

  14. Hey, just wanted to say thanks heaps for the generous sharing of info. I’m very much a beginner, and I was seriously struggling with the Winforms samples on the Creator’s Club site. Then I stumbled across this and got something working in minutes. Thankyou!

    Comment by Boon | June 20, 2009 | Reply

  15. Glad it was helpful. I do plan to expand on this series, just have been very busy lately.

    Comment by roecode | June 22, 2009 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: