Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML within windows forms  (Read 33297 times)

0 Members and 1 Guest are viewing this topic.

Glenn

  • Newbie
  • *
  • Posts: 12
    • View Profile
SFML within windows forms
« on: September 14, 2012, 12:11:45 am »
Could someone create a tutorial on how to create a sfml window within windows forms? I want to use sfml .Net
and c# windows forms to create a map editor, but I really don't have a clue how to integrate the two.

I searched the forums and found a handful of threads, but those didn't really help. I imagine there are other people who want to do the same thing, but don't know how.

Thanks

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: SFML within windows forms
« Reply #1 on: September 14, 2012, 12:20:37 am »
Have you looked at the examples that come with SFML.Net?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Glenn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML within windows forms
« Reply #2 on: September 14, 2012, 12:31:44 am »
I had not, but on after doing a quick overview, none of those seem to be an example of what I want to do. I know how to use SFML.NET, it's just rendering it within windows forms that I am lost.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: SFML within windows forms
« Reply #3 on: September 14, 2012, 01:17:54 am »
This is really simple to do, just use the Control.Handle of any win forms control. Form, Panel control, ect.... To make a new SFML.Graphics.RenderWindow. If you need a code example I can put something together for you.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Glenn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML within windows forms
« Reply #4 on: September 14, 2012, 02:03:21 am »
A code sample would be awesome if you wouldn't mind, that would be great  :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML within windows forms
« Reply #5 on: September 14, 2012, 07:54:51 am »
As far as I remember, there are such examples on the .Net forum.
Laurent Gomila - SFML developer

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: SFML within windows forms
« Reply #6 on: September 14, 2012, 03:04:57 pm »
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);
        }
    }
« Last Edit: January 20, 2014, 01:01:54 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Glenn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML within windows forms
« Reply #7 on: September 14, 2012, 05:53:14 pm »
zsbzsb, Thank you very much! This is exactly what I was looking for.

miotto

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: SFML within windows forms
« Reply #8 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);
        }
    }
}
 

« Last Edit: January 28, 2017, 07:42:12 pm by miotto »

PL_Andrev

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: SFML within windows forms
« Reply #9 on: May 24, 2019, 09:23:38 pm »
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)
        {
        }
    }
}

davzone

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: SFML within windows forms
« Reply #10 on: May 12, 2021, 08:32:32 pm »
 

                // all the pre drawing code ignored
                renderwindow.Draw(rec); // ---> but is not shown !!!
               //  **** you are missing ****
                renderwindow.Display();
 

 

anything