1
DotNet / Re: SFML within windows forms
« on: January 28, 2017, 05:33:35 pm »
I also wanted something similar. I created an UserControl (I named it SFMLCanvas) and added a BackgroundWorker (I named it RenderLoopWorker). This way I can drag and drop this control on a form whenever I need it.
I should mention that I'm a beginner, so could be that this code is not the best way to do it.
using SFML;
using SFML.System;
using SFML.Graphics;
using SFML.Window;
namespace EditProp3D
{
public partial class SFMLcanvas : UserControl
{
private RenderWindow RendWind;
public SFMLcanvas()
{
InitializeComponent();
}
public void StartSLMF()
{
RenderLoopWorker.RunWorkerAsync(this.Handle); //RenderLoopWorker is a BackgroundWorker
}
private void DrawStuff()
{
CircleShape CS = new CircleShape(10);
CS.FillColor = Color.Magenta;
RendWind.Draw(CS);
}
private void RenderLoopWorker_DoWork(object sender, DoWorkEventArgs e)
{
RendWind = new RenderWindow((IntPtr)e.Argument);
while (RendWind.IsOpen)
{
RendWind.DispatchEvents();
RendWind.Clear(new Color((byte)51, (byte)77, (byte)102));
DrawStuff();
RendWind.Display();
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if (RendWind == null)
base.OnPaint(e);
}
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
{
if (RendWind == null)
base.OnPaintBackground(pevent);
}
}
}
I should mention that I'm a beginner, so could be that this code is not the best way to do it.
using SFML;
using SFML.System;
using SFML.Graphics;
using SFML.Window;
namespace EditProp3D
{
public partial class SFMLcanvas : UserControl
{
private RenderWindow RendWind;
public SFMLcanvas()
{
InitializeComponent();
}
public void StartSLMF()
{
RenderLoopWorker.RunWorkerAsync(this.Handle); //RenderLoopWorker is a BackgroundWorker
}
private void DrawStuff()
{
CircleShape CS = new CircleShape(10);
CS.FillColor = Color.Magenta;
RendWind.Draw(CS);
}
private void RenderLoopWorker_DoWork(object sender, DoWorkEventArgs e)
{
RendWind = new RenderWindow((IntPtr)e.Argument);
while (RendWind.IsOpen)
{
RendWind.DispatchEvents();
RendWind.Clear(new Color((byte)51, (byte)77, (byte)102));
DrawStuff();
RendWind.Display();
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if (RendWind == null)
base.OnPaint(e);
}
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
{
if (RendWind == null)
base.OnPaintBackground(pevent);
}
}
}