I’m currently working on a tilemapeditor using monogame and winforms. I got monogame using together with winforms by using the xna winforms sample graphicsdevicecontrol etc and ported it to monogame using opentk. This far it workes fine, however, when I try to draw something I only get a black window in the form. For example I tried to clear the graphicsdevice with a blue color but I get a black window in the winforms form.
The code I have:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace TileMapEditor
{
class Editor : GraphicsDeviceControl
{
ContentManager content;
SpriteBatch spriteBatch;
Map map;
Texture2D t;
public Editor()
{
map = new Map();
}
protected override void Initialize()
{
content = new ContentManager(Services, "Content");
spriteBatch = new SpriteBatch(GraphicsDevice);
t = content.Load<Texture2D>("Gameplay/Tank1");
XmlSerializer xml = new XmlSerializer(map.GetType());
Stream stream = File.Open("Load/Map.xml", FileMode.Open);
map = (Map)xml.Deserialize(stream);
map.Initialize(content);
}
protected override void Draw()
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(t, new Rectangle(10, 10, t.Width, t.Height), Color.White);
spriteBatch.End();
}
}
}
I’m going to call a drawfunction in the map class later, the texture t here is just a test. I follow this tutorial to port the xna sample to monogame.
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
PS: To anyone wondering why my random post, the person asking the question requested i post this here.
Hey Damien, I’ve had a few goes at getting your control to work for me, but unfortunately as I have virtually no Winforms experience, i’m getting stuck.
Any chance you could pop a super basic example up onto your GitHub?
I’ve got VS2015 Community, I’ve tried creating a new winforms application, getting OpenTK.GLControl and your MonoGame.Framework.Control from NuGet, but I’m unable to select a GraphicsDeviceControl from the toolbox to add to the form layout.
My goal is to try and update/replicate Gleed2D which I’ve used as a level editor in the past.
Any help (or a basic example) would be greatly appreciated!
@marksarcade Before you can select a custom control from the toolbox in winforms, you have to import the control to also be inside the toolbox using the “Choose Toolbox Item” dialog ( https://msdn.microsoft.com/en-us/library/ms165355(v=vs.100).aspx) . You have to add the reference to the .dll file from the packages folder (from NuGet) inside this dialog before you can use the control from the toolbox.
Thanks @MasterQuestMaster. I have tried that, but all I get is the OpenTK.GLControl control, not the GraphicsDeviceControl mentioned in the post above, which is why a bit confused. I can see in the Object Browser on the referenced Monogame.Framework.Control library, the GraphicsDeviceControl as well as the OpenTK.GLControl.
I dropped my project version back to 4.5 from 4.5.2, but that didn’t seem to help.
@marksarcade Did you create a class that inherit GraphicsDeviceControl or are you trying to add GraphicsDeviceControl itself? You have to create a class that inherit GraphicsDeviceControl, build the application and then the class should show up in the toolbox. I can give you an example later today if you still can’t get it working
I tried that a few ways as well (both with the Monogame.Framework and the Microsoft.Xna.Framework version of the Control) and can’t seem to get any work
A simple example would be REALLY appreciated! I’m stumped as to what i’m doing wrong!?!?
Thanks @TS1997, that works. I was trying to use the NuGet package which I thought was the way to go, I guess I should have just tried downloading the code and creating the control myself!
Depends on which version of the control you were grabbing… if it was based on the Xna implementation you have to make a class that inherits the GraphicsDeviceControl in order to make a control appear in the toolbox… if you grabbed the Latest Control.DirectX version then you can use the render event on a GraphicsDeviceControl rather than having to inherit one to make a toolbox item.
That’s exactly the approach I’m working on right now.
This piece of code is going to be very useful, Thanks a lot.
I’ll publish the Winform version very soon.