XNA Windows.Forms UserControl

Notice

This article is outdated!
This was a pretty cool achievement at the time, but XNA 1.0 is long gone. Microsoft now offers their own examples on how to do this:
WinForms Series 1: Graphics Device
WinForms Series 2: Content Loading

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:

An XNA-based pong clone running inside a Windows.Forms application

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please copy the string pTu1mI to the field below:

This site uses Akismet to reduce spam. Learn how your comment data is processed.