MonoGame.Forms.DX 3.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MonoGame.Forms.DX --version 3.1.0
NuGet\Install-Package MonoGame.Forms.DX -Version 3.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MonoGame.Forms.DX" Version="3.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MonoGame.Forms.DX --version 3.1.0
#r "nuget: MonoGame.Forms.DX, 3.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MonoGame.Forms.DX as a Cake Addin
#addin nuget:?package=MonoGame.Forms.DX&version=3.1.0

// Install MonoGame.Forms.DX as a Cake Tool
#tool nuget:?package=MonoGame.Forms.DX&version=3.1.0

Banner

Welcome to MonoGame.Forms!

Wiki

Visual Studio

NuGet

NuGet

MonoGame.Forms is the easiest way of integrating a MonoGame render window into your Windows Forms project. It should make your life much easier, when you want to create your own editor environment.

Building

  • The MonoGame.Forms project uses a modified version of the MonoGame.Framework. It's called MonoGame.Framework.WindowsDX.9000 (created by nkast), which is faster, memory optimized, bugfixed and supports full mouse & keyboard input within WindowsForms. You can also update MonoGame.Forms to a new MonoGame version very easily - just by updating the MonoGame.Framework.WindowsDX.9000 nuget package!
  • MonoGame.Forms.GL - DEPRECATED! - faster alternative: MonoGame.Template.Gtk.CSharp (created by harry-cpp).
Tips & Tricks / Dos & Don'ts
  • Never use DoubleBuffering on a custom control. It will cause flickering and slow downs.
  • If you experience scaling issues with your drawn content, then you might want to set the right AutoScaleMode of a Form containing a MonoGameControl: AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;. If you want to turn off scaling of your whole application, then you need to add a Manifest-File. NEW: Now you can also directly install the Visual Studio templates.

How-To

Setup MonoGame.Forms

First you need to make your MonoGame.Forms library ready to use. This step is very easy; you just need to compile the MonoGame.Forms library from source and then just add the compiled DLL's to your project.

This is the prefered route, when you want to make you own custom changes to the library or extend it.

Another option is to install the library with the NuGet package manager:

NuGet

This is the prefered and easiest route to be automatically up to date.


Tutorial

Creating a simple MonoGameControl

Let's start using the MonoGame.Forms library by creating a simple control to render stuff!

(it's assumed that you already have created a new Windows Forms project with the installed library)

  1. Create a new class and name it DrawTest
  2. Inherit from MonoGame.Forms.Controls.MonoGameControl
  3. Override its Initialize(), Update() and Draw() method
  4. Save your solution
  5. Build your solution
  6. Double Click on Form1.cs so that the designer opens
  7. Open the Toolbox
  8. Drag & Drop the newly created control onto the Form1 control
  9. Open the Properties of the new control and set the Dock option to Fill

This is how it should look now:

Tutorial

  1. Now run the solution and see the classical CornflowerBlue-Screen you are (surly) familiar with! 😉

Tutorial

And yes, as you can see: it is realy THAT EASY!

Now I bet you wonder how to draw something to this control, right? I bet you think that this is now the difficult part, right? Well... it's not!

More than that it's basically the same like you are used to do in the MonoGame.Framework. Just with a small difference (no it's still not difficult!)

In MonoGame you could draw someting to the screen with the SpriteBatch. In MonoGame.Forms you will do the same but you need to use the GFXService for this.

In the MonoGameControl class this service is called Editor. To draw something to the SpriteBatch you need to do this:

Editor.spriteBatch.DrawString();

Do you see? Easy! 😃

The GFXService class contains some MonoGame specific stuff like a ContentManager. Examine everything calmly. I just want to explain a little how MonoGame.Forms works under the hood!

To sum things up, let's take a look at the final DrawTest class:

using Microsoft.Xna.Framework;
using MonoGame.Forms.Controls;

namespace nugetTest
{
    public class DrawTest : MonoGameControl
    {
        string WelcomeMessage = "Hello MonoGame.Forms!";

        protected override void Initialize()
        {
            base.Initialize();
        }
        
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        protected override void Draw()
        {
            base.Draw();

            Editor.spriteBatch.Begin();

            Editor.spriteBatch.DrawString(Editor.Font, WelcomeMessage, new Vector2(
                (Editor.graphics.Viewport.Width / 2) - (Editor.Font.MeasureString(WelcomeMessage).X / 2),
                (Editor.graphics.Viewport.Height / 2) - (Editor.FontHeight / 2)),
                Color.White);

            Editor.spriteBatch.End();
        }
    }
}

Result:

Tutorial

It's pretty much like in the MonoGame.Framework!

I just want to refer to the nice MonoGame.Forms.Samples-Project, which is part of this repo. Take a look at it and learn from its samples.

BTW: did you notice the BackColor and ForeColor property? Changing these values makes it possible to style your controls to something like this:

Style

Do it to keep the overview and feel of your custom editor project!

Note: The MonoGame logo is placed automatically inside a newly created control (during design-time) to make it clear, that it is a render control with MonoGame functionality!


Creating a simple InvalidationControl

This specific control class doesn't need to override the Update() method, because it gets manually invalidated (by you!).

If you are changing the drawn contents of the SpriteBatch when your editor project is running (not during design time), then you simply need to call Invalidate() on a custom control for every change you want to see on your control. This command commits those changes and after that your control does not consume CPU power anymore. This process is great when creating preview controls for textures and similar things!

Sample Pics

Here are some pics of some samples included with the repo:

Windows

Sample

Projects using MonoGame.Forms!

Please watch the following YouTube videos in 1080p @ 60fps to see what is possible with MonoGame.Forms!

This project is called: "Rogue Engine Editor" and it's possible to create Rogue Adventures with it:

Rogue Engine Editor


This project is called: "PenumbraPhysics.Editor" and it was the prototype for the Rogue Engine Editor and MonoGame.Forms:

YouTube Video

GitHub: PenumbraPhysics.Editor


Now Have Fun with MonoGame.Forms!

Logo

Special Thanks

Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed. 
.NET Framework net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on MonoGame.Forms.DX:

Repository Stars
BlizzCrafter/MonoGame.Forms
MonoGame.Forms is the easiest way of integrating a MonoGame render window into your Windows Forms project. It should make your life much easier, when you want to create your own editor environment.
lofcz/SimplexRpgEngine
Modular game engine built with MonoGame, with GMS2-like workflow and advanced level editor
Version Downloads Last updated
3.2.0 655 8/25/2023
3.1.0 268 8/16/2023
3.0.0 1,293 4/21/2022
2.4.0 2,110 8/4/2020
2.3.9 1,113 2/24/2020
2.3.8 1,284 7/11/2019
2.3.7 1,306 6/10/2019
2.3.6 670 4/26/2019
2.3.5 661 3/28/2019
2.3.4 617 3/28/2019
2.3.3 605 3/25/2019
2.3.2 614 3/13/2019
2.3.1 777 1/25/2019
2.3.0.1 682 1/23/2019
2.3.0 759 1/19/2019
2.2.1 793 1/12/2019
2.2.0 766 1/4/2019
2.1.0.2 1,097 10/19/2018
2.1.0.1 839 9/27/2018
2.1.0 938 8/18/2018
2.0.0.1 861 8/18/2018
2.0.0 999 7/16/2018