Search code examples
c#winformsmonogameopentk

Use monogame with winforms


I'm currently trying to create a mapeditor using monogame and winforms in C#. I'm working in Visual Studio 2015 on a Windows 7 machine. I created a winforms project and proceeded by adding the following references: MonoGame.Framework, OpenTk and OpenTK.GLControl. Then I included the following files from the XNA Winforms Sample: GraphicsDeviceControl.cs, GraphicsDeviceService.cs and ServiceContainer.cs. When this was done I followed this tutorial to port the XNA Sample to MonoGame. This far everything works.

Then I created this test class to see that it works:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TileMapEditor
{
    class Test : GraphicsDeviceControl
    {
        ContentManager content;
        SpriteBatch spriteBatch;
        Texture2D t;

        protected override void Initialize()
        {
            content = new ContentManager(Services, "Content");
            spriteBatch = new SpriteBatch(GraphicsDevice);

            t = content.Load<Texture2D>("Gameplay/Player");
        }

        protected override void Draw()
        {
            spriteBatch.Begin();
            spriteBatch.Draw(t, new Rectangle(10, 10, t.Width, t.Height), Color.White);
            spriteBatch.End();
        }
    }
}

Then I built the project so this class would show up in the forms designer toolbox. This worked as well so I proceeded by draging the class onto the form in the designer. This showed up as normal as well. Then I built the project once again and hoped that my texture would show up, however, I only got a black window as big as the class I added in the designer, see this picture: prnt.sc/b33my9 (Can't make a 3rd link since I don't have enough rep) I can also mention that Visual Studio 2015 says that the field 'GraphicsDeviceService.parameters' in GraphicsDeviceService.cs is never used. I don't know if that is a part of my problem or not. I also tried to replace my code in the Draw function with this line:

GraphicsDevice.Clear(Color.Blue);

but I still got the same black window. I've also tried the ported xna sample in the MGWinFormsControls project located in this gitub repo: github.com/jaquadro/MonoGame-WinFormsControls and the result is that the 'GraphicsDeviceService.parameters' warning is gone but I still can't get anything except a black window.

Is there anyone that know what I should do to fix this?

Thanks in advance!


Solution

  • Jaquadro edition is for OpenGL only. It is based on an older edition of MonoGame where GL + winforms was possible. The head developer currently has this ability disabled on purpose until further notice but DX is compatible with it. I have posted 2 libraries that accomplish this.

    This is based on the Xna tutorial DIRECTLY PORTED to monogame. https://github.com/ArchaicOokami/MonoGame.Framework.Control

    This one is based on a modification to the above version. https://github.com/ArchaicOokami/Microsoft.Xna.Framework.Control

    In the Microsoft Xna Framework Control - based on the xna forms control example - You have to have 3 classes. - GraphicsControlDevice - GraphicsDeviceService - Service Container

    When using this you will create a new class, inherit GraphicsDeviceControl(Which in turn is making a UserControl) and modify the draw function to perform your task. After having this ready and throwing it on a form, which requires a whole new control per window(even if they only slightly differ) as well as to call somewhere in the code to draw. This can be done by adding the draw code to Application.Idle, or by manually calling the draw within a main loop.

    The current compiled edition in my git post is targetting .net 4.0 and uses MonoGame 3.4.

    While using my modified library, MonoGame Framework Control all you need to do is put a GraphicsDeviceControl onto your form. Then add the Draw event from visual designer and add you code there. The clearcolor is done automatically before draw is called and can be set via code or visual designer as it is a property you can modify from the Properties window. In order to make this one draw all you have to do is set AutoDraw to true from the visual designer or set it to false and manually call the draw inside of a loop.

    The current compiled edition in my git post is targetting .net 4.5 and uses MonoGame 3.5.

    Both of these can also be found on Nuget by searching ArchaicSoft or the names they are listed as in the git aka - MonoGame.Framework.Control Microsoft.Xna.Framework.Control

    Personally for ease of use + convenience, i recommend the one I modified for a Windows style forms control instead of a usercontrol experience but that is your choice.