Geeks With Blogs

News Bob Taco Industries is an ISV focused on game and app development for Microsoft platforms headed up by Michael B. McLaughlin. Mike is a Microsoft Visual C++ MVP (previously an XNA/DirectX MVP from 2011-2013), a developer, a writer, a consultant, and a retired lawyer. If you're a developer who is just getting started, consider checking out the BTI website's section for developers for links to code samples and other helpful sites.
Bob Taco Industries Blogging Division

XNA Games in Windows are hosted within a Windows Forms Form. This allows you access to many special Windows-only features, such as drag and drop, provided that you know the right code to put in to get access to that form.

Someone on the App Hub forums had asked earlier today about how to enable drag and drop for a Windows-only XNA game. Since it sounded like a neat thing to learn how to do, I coded up a quick sample to display it. As always, the code is heavily commented so that it should be easy to follow along with. It’s licensed for use under the terms of the Microsoft Public License.

A few key notes. First, note that in the Program.cs file (where the static Main method that marks the entry point for the game is), I had to add an STAThreadAttribute to the Main method, since drag and drop requires the COM single threaded apartment threading model. I’m unaware of any negative repercussions that might have for an ordinary game (or editor or whatever you decide to build), but I wanted to note that since I doubt most people ever look at Program.cs. Also, I had to add a reference to System.Windows.Forms to the game project’s References in Solution Explorer.

The game will accept a drag and drop of most text (note that for reasons I have never been able to divine, you cannot drag and drop text from IE into anything other than IE – this is likely a by-product of a security thing, but I just don’t know). It will also accept a drag and drop of an image file! It only accepts either as a copy, not as a move, so there shouldn’t be any problems with that. I only implemented the bare minimum of error mitigation to prevent crashes. I did not implement proper error handling and the comments in the code reflect that. Drag and drop is enabled within the Initialize method of the Game1 class and event handlers for it are set there. If you drag text (e.g. from Word or WordPad), it will display the text (I enabled the DefaultCharacter in the SpriteFont to prevent crashes in case a particular character doesn’t exist in the font). If you drag and drop a file, it will attempt to load it as a texture and display it underneath where any text would be drawn. Note that only PNG, JPG, and GIF files will work and then only if the image’s dimensions are no greater than 2048x2048 (I switched the game to the Reach graphics profile in the game’s properties to enable as many people as possible to use it without needing to jump through that hoop).

Ok, without further rambling, here it is: http://www.bobtacoindustries.com/developers/utils/DragAndDropGame.zip .

For an example of Windows Forms drag and drop, see the sample code here: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop.aspx .

For a listing of the possible data formats the drag and drop allows, see: http://msdn.microsoft.com/en-us/library/system.windows.forms.dataformats_fields.aspx . You may need to search the internet a bit to figure out how some of them work (I had to do so to figure out that the FileDrop format returns a string array, for example).

Posted on Sunday, March 27, 2011 2:05 PM xna , C# , tutorial | Back to top


Comments on this post: Drag and Drop in a Windows XNA Game

# re: Drag and Drop in a Windows XNA Game
Requesting Gravatar...
The STAThreadAttribute can cause problems for multithreading, if you use certain synchronization techniques.

For example, WaitHandle.WaitAll() will no longer work with [STAThread].

Anyway, sweet post.
Left by Colinshark on Mar 27, 2011 10:49 PM

Comments have been closed on this topic.
Copyright © Michael B. McLaughlin | Powered by: GeeksWithBlogs.net