XNA Windows.Forms UserControl

in

A topic that pops up every now and then is how to use XNA in a Windows.Forms application. This is rather difficult because the GraphicsDeviceManager provided by Microsoft creates its own window and will not cooperate with a normal Windows.Forms window, thus forcing the developer to roll his own GraphicsDevice initialization and management code (which is quite a bit of work!)

As can be seen in one of my recent news posts, I did just that while developing a world editor for my upcoming game Island War. Because of the great demand for such a component, I decided to release my XNA GameControl class to the public. You can download it here:

www.nuclex.org/downloads/developers/kits/xna-usercontrol

Screenshot of a Pong game running in a Windows.Forms window

How to use:

  1. Add a reference to Nuclex.GameControl.dll (or to the project if you integrated the project file into your solution)
  2. Create a new UserControl that inherits from Nuclex.GameControl. You can do this by adding a normal UserControl and replacing the : UserControl by : Nuclex.GameControl in the class definition.
  3. Override the LoadGraphicsContent(), UnloadGraphicsContent(), Update(), Draw() and so on methods as usual.

There's one issue: Because the working directory will be set to the system's temp folder when your control is shown in designer mode, you will not be able to view your game in the Visual Studio Forms Designer if it is loading content using the XNA content pipeline (well, unless you want to copy your project's output folder into your temp directory whole). There will be no problems starting or debugging the project, though.

pyrokar's picture

thanks a lot!

Thanks a lot.

This source code really helps me developing my game :)

You saved my day.

 

Keep up the good work

Fallen Angel's picture

THANKS!

That is really, really helpfull - i searched for something like this for a long time.

I thought about creating a System.Drawing-wrapper for the xna-sprite batch code, but this is a lot more useful to develop my game!

 Thanks a lot!

Tiptup300's picture

Its "though" not "thought" I

Its "though" not "thought" I noticed you have the "t" in a couple articles and some things you posted. For some reason it REALLY bothered me. I'm not a grammer/spelling nazi, but for some odd reason, I just couldnt stand the "t" lol.

Cygon's picture

Will fix it :)

Heh, thanks. I'm german btw, and that must have somehow slipped into my vocabulary :)

Tim's picture

I have something similar to this

Very nice! I just completed something similar to what you have here. I have a control called GameViewport that you can add to a form. It's a simple user-drawn control that's really just a placeholder for the XNA game form. When the program runs, my customized HostedGame looks for the GameViewport control and replaces it with a game form inside the host form. My project is at http://www.codeplex.com/rigamarole if you want to check it out and let me know what you think.

Oh! Almost forgot to mention... it's written in VB.Net

Thanks and keep up the great work!

Tim

Cygon's picture

There are lots of SetParent-Controls out there...

I specifically shied away from your approach because the original Game class runs a loop after the WM_IDLE message. As soon as you have more than one Game class active, the first instance's loop will hog the WM_IDLE event and not allow the other instances to update until another window message breaks the loop.

My GameControl uses a modified idle loop that works with as many instances as you like. The drawback with my code then is that it's no longer the original XNA game class and you can't create any GameComponents on it which require passing the game instance in their constructor...

Steve Montgomery's picture

Fixing the design mode exceptions

Thanks for this excellent piece of work. One thing though... anyone who is using this control can fix the design mode exceptions by wrapping the code in LoadGraphicsContent() and Draw() in an if (!this.DesignMode){} block. Basically, that test will only allow the code to execute if it is not being called in the design mode.

Anonymous's picture

XNA 2.0 version?

Is there any plans for a version compatible with XNA 2.0?

aoc gold's picture

Any updates on XNA 2.0

Any updates on XNA 2.0 compatibility?

Cygon's picture

Microsoft has its own solution now

Microsoft is providing a GraphicsDeviceControl for windows forms themselves now, so I didn't put any more work into this.

Check it out here:
WinForms Series 1: Graphics Device

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <br> <a> <em> <strong> <u> <i> <b> <cite> <blockcode> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <pre> <span>
  • You can highlight code with any of the following tags: <blockcode>

More information about formatting options