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: UserControlby: Nuclex.GameControlin 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.
thanks a lot!
Thanks a lot.
This source code really helps me developing my game :)
You saved my day.
Keep up the good work
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!
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.
Will fix it :)
Heh, thanks. I'm german btw, and that must have somehow slipped into my vocabulary :)
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
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...
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.
XNA 2.0 version?
Is there any plans for a version compatible with XNA 2.0?
Any updates on XNA 2.0
Any updates on XNA 2.0 compatibility?
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