Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Is it possible to keep a Windows Form inside a XNA project? I tried it but without success.

Below I'm showing how my main file is.

using System;

namespace JogoSuecaOnline
{
#if WINDOWS || XBOX
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            MainMenu menu = new MainMenu();
            menu.Show();
        }
    }
#endif
}
share|improve this question
7  
Your code sample is almost entirely useless. –  The Communist Duck Jun 4 '11 at 17:21
    
Also, you will have more success with XNA inside Winforms. Wait, why are you still using Winforms over WPF? (or the new-ish windowing one) –  The Communist Duck Jun 4 '11 at 17:23
1  
Nothing wrong with WinForms. –  Andrew Russell Jun 4 '11 at 17:25
    
@Andrew Russell I cannot give a source, but I remember reading to use WPF/the newer one over WinForms for some reason. –  The Communist Duck Jun 4 '11 at 19:54
1  
Uh... that's not a very good reason at all. WPF integration is possible, it's just not quite as easy as WinForms (see those two linked samples). Neither is otherwise "better" than the other. –  Andrew Russell Jun 5 '11 at 3:06

3 Answers 3

up vote 2 down vote accepted

Try this in your main file:

   static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new YourForm());
    }
share|improve this answer

There are two options. First of all you could embed an XNA view inside a WinForm. For information about how to do this: download, read through the documentation for, and play with the project files in:


The other thing you might want to do is to launch a WinForm from within an XNA game (so your game is running, and then the form is shown next to it). This is pretty easy: right click your game project -> Add -> Windows Form. This will set up the necessary references and create a template form for you.

To launch your form, simply create and show it. Here's an example to launch the form when your Game starts:

Form1 form;
protected override void Initialize()
{
    form = new Form1();
    form.Show();
    base.Initialize();
}
share|improve this answer

Alright this might be old as hell, but I was having trouble with this and I found an answer.

The example project provided by Microsoft (XNA WinForms Sample 1) includes three main classes that you need in order to MAKE a control.

  • GraphicsDeviceControl.cs
  • GraphicsDeviceService.cs
  • ServiceContainer.cs

Starting with a fresh windows form project you will need to make references to Microsoft.Xna.Framework and Microsoft.Xna.Framework.Graphics. Now make a reference to your XNA project where your XNA game is located (or game library).

Copy over those three files and change the namespace on those three classes to your windows form namespace. Then you will want to make a NEW class which inherits from GraphicsDeviceControl.

Now modify the using statements to include the XNA libraries you need. Then add the overrides for Initialize, Dispose, Draw, and any other methods you will be needing to modify.

e.g.

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace WindowsFormsApplication1
{
    public class MapControl : GraphicsDeviceControl
    {
        protected override void Initialize()
        {
            //throw new System.NotImplementedException();
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
        protected override void Draw()
        {
            //throw new System.NotImplementedException();
        }
    }
}

Now compile the windows form project and your new control will appear in the toolbox.

This all came about because I couldn't figure out why I kept getting errors when trying to place the abstract base class GraphicsDeviceControl on the form, but it's taken me 2 years to actually figure out why.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.