Search This Blog

TopMeny

Thursday, December 13, 2007

DirectX as UserControl in a Windows Form

How to insert a DirectX render-window into a Windows Form application using the DirectX sample framework.
Keywords: DirectX, sample framework, windows form, UserControl.
Download source code and executable
directxasusercontrol.jpg
I found it very hard to find a good example of how to have a DirectX window inside a Windows Form application and still have standard Windows Form controls without making DirectX lock the GUI. I also wanted to use a sample framework application from the DirectX samples.
There is a very good example (CML viewer) of using a DirectX sample framework in a WinForm and I have looked at it a lot when creating this example. Anyway, I wanted to do it my way so I did my own version of it. I also wanted the rendering-loop to be optimal and it actually is in the later versions of the sample framework. It is using the ApplicationIdle-event to render, read more about optimal rendering-loops at Tom Millers blog.
Note that the sample framework is not created to be used like this so you may want to consider creating the DirectX-stuff yourself without the sample framework.
Sometimes the applications has problems at start up, maybe because of the LoaderLock turned off, does anyone have a solution? For LoaderLock problems, see this thread.
I have not put much effort in documentation and super pretty code (all my time goes to my master thesis projekt) but it is an implementation that work and I don´t think it is that hard to understand.

No comments: