I am pretty sure, that like me you are more than capable of scanning the web for snippets of info on how to do this, well I thought, to save you the time and effort I’ll put up a simple how-to here.
As ever I am standing on the shoulder of giants, the first thing I do to get multi-touch into my XNA project is to use a regular windows form to render to. I dare say there it a much better way to do this with WPF, which I am sure I’ll have a look at when I have time, but for now, I am using the good old 2.0 Windows Form. Now for this I have used a great post by another XNA MVP Pedro Guida aka “Ultrahead” showing how to get XNA to render to a panel control. I can’t for the life of me find his original post, it may have even been on The Code Project and also used the MS Multitouch code samples.
The spawn of these two resources has given me my own Panel control that XNA can render to AND the Windows 7 Multi-touch API can be hooked up to.
So, how does all this hang together first off we open up a new XNA Windows Project, then Add a Windows Form to the project, we then need to create our own Panel object so that it can interact with multi-touch.
The code for the new Panel class is 99% taken from the MS Multitouch samples and it involves hooking into the WndProc method on the control.
So now we have a control we can use to get MT input for, we now need to hook this control up to our form and in turn to our XNA Game class. In the form we need to give our Game class access to the handle it is going to render to. To do this we define XNAWindowsMTPanel object, create a Property on the form so that it can be accessed and then instantiate the XNAWindowsMTPanel object. This leaves our form source code looking like this:
Now in our game class we need to set up the Windows form to be the form we will be rendering to, we create a From object, in the constructor, instantiate, bind to the forms HandleDestroyed event so when the form closes we can tidy up and then show the form. We also need to be able to ‘hide’ our XNA game window to so to do this we need to bind to the XNA game windows Shown event and then hide the window. To help us do this we will need to reference System.Windows.Forms in our Game1 class and to stop any name space conflicts we will alias it with ‘SysWinForms’. We also need to set our view port so that it matches the area we are rendering to and this also needs to be done if the form is resized.
The top of our Game1.cs file now looks like this:
Running this now will just give us a blank Windows Form, we need to now tell our Game1 class to render to our XNAWindowsMTPanel and this is very simple to do, simply at the end of the Draw call do this:
You will now have your XNA code rendering on the windows form!! How cool is that!
Now to add the MT functionality. You can see on the XNAWindowsMTPanel class that we have some events we can bind to:
So we we will get some code stubs in our Game1 class then wire these up in Windows Form (again taken from the MS MT samples)
And now to wire them up in the form:
So, we now have our XNA project rendering on a windows form that is also able to accept munti touch commands, so how can we demonstrate this?
First of all we are going to create an object that we can use to store the touch events in, this will also hold some data to help us represent the touch on the screen.
We then create a Dictionary to store these items in, I am also going to add a Texture2D to render the touch objects and give an audio que with a SoundEffect Object. This is hoe I have defined them in the Game1 class:
Now all we need to do is record when we have a touch contact, when one is removed and when one is moved. Our touch event handlers now look like this:
I then make sure that the array of points I am using to represent the touch contacts is updated, effectively giving a string of particles after the touch contact point, giving us the following Update and Draw methods:
And that’s about it, hope you find it useful. The solution for this post can be found here. As ever C&C welcome.