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:
How to use:
- Add a reference to Nuclex.GameControl.dll (or to the project if you integrated the project file into your solution)
-
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. -
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.