Just paste this into any new C# Win Forms Application. I just threw this code together for behind a form named Form1.
If you need a VB.NET example or have more questions feel free to ask.
class Example
{ static void Main
() { // initialize the form System.Windows.Forms.Form form
= new System.Windows.Forms.Form(); // create our form form
.Size = new System.Drawing.Size(600,
600); // set form size to 600 width & 600 height form
.Show(); // show our form DrawingSurface rendersurface
= new DrawingSurface
(); // our control for SFML to draw on rendersurface
.Size = new System.Drawing.Size(400,
400); // set our SFML surface control size to be 500 width & 500 height form
.Controls.Add(rendersurface
); // add the SFML surface control to our form rendersurface
.Location = new System.Drawing.Point(100,
100); // center our control on the form // initialize sfml SFML
.Graphics.RenderWindow renderwindow
= new SFML
.Graphics.RenderWindow(rendersurface
.Handle); // creates our SFML RenderWindow on our surface control // drawing loop while (form
.Visible) // loop while the window is open { System.Windows.Forms.Application.DoEvents(); // handle form events renderwindow
.DispatchEvents(); // handle SFML events - NOTE this is still required when SFML is hosted in another window renderwindow
.Clear(SFML
.Graphics.Color.Yellow); // clear our SFML RenderWindow renderwindow
.Display(); // display what SFML has drawn to the screen } } } public class DrawingSurface
: System.Windows.Forms.Control { protected override void OnPaint
(System.Windows.Forms.PaintEventArgs e
) { // don't call base.OnPaint(e) to prevent forground painting // base.OnPaint(e); } protected override void OnPaintBackground
(System.Windows.Forms.PaintEventArgs pevent
) { // don't call base.OnPaintBackground(e) to prevent background painting //base.OnPaintBackground(pevent); } }
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
); } }}
Hello,
I tried conect SFML window into already designed winform to overlap not-used panel.
Unfortunatelly the result is white window (instead black) and rectangle is not shown on them.
What do I wrong?
namespace SFML_test
{ public partial class Form1
: Form
{ public Form1
() { InitializeComponent
(); DrawingSurface rendersurface
= new DrawingSurface
(); rendersurface
.Size = new System.Drawing.Size(400,
200); splitContainer4
.Panel1.Controls.Add(rendersurface
); rendersurface
.Location = new System.Drawing.Point(0,
0); // initialize sfml SFML
.Graphics.RenderWindow renderwindow
= new SFML
.Graphics.RenderWindow(rendersurface
.Handle); // drawing loop while (splitContainer4
.Panel1.Visible) { System.Windows.Forms.Application.DoEvents(); renderwindow
.DispatchEvents(); renderwindow
.Clear(SFML
.Graphics.Color.Black); // ---> but is white !!! renderwindow
.Display(); RectangleShape rec
= new RectangleShape
(); rec
.Position = new Vector2f
(10,
10); rec
.Size = new Vector2f
(20,
20); rec
.OutlineThickness = 1; rec
.FillColor = SFML
.Graphics.Color.Blue; rec
.OutlineColor = SFML
.Graphics.Color.Red; renderwindow
.Draw(rec
); // ---> but is not shown !!! } } } public class DrawingSurface
: System.Windows.Forms.Control { protected override void OnPaint
(System.Windows.Forms.PaintEventArgs e
) { } protected override void OnPaintBackground
(System.Windows.Forms.PaintEventArgs pevent
) { } }}
// all the pre drawing code ignored
renderwindow.Draw(rec); // ---> but is not shown !!!
// **** you are missing ****
renderwindow.Display();