SFML community forums

Bindings - other languages => DotNet => Topic started by: nickkorta on August 01, 2013, 02:56:33 am

Title: Embed SFML Window in picturebox(Answered)
Post by: nickkorta on August 01, 2013, 02:56:33 am
Hi I'm using SFML.net 2.1. I'm trying to get a SFML window inside a picturebox but I am having no luck.

 public partial class Form1 : Form
    {
        private RenderWindow mapWindow;
        private bool running = true;

        public Form1()
        {
            InitializeComponent();
         
        }

        private void Draw()
        {
            Application.DoEvents();
            mapWindow.Clear(new SFML.Graphics.Color(101, 156, 239));
            mapWindow.Display();
            pictureBox1.Refresh();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            running = false;
        }

        private void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            mapWindow = new RenderWindow(this.pictureBox1.Handle);
            while (running)
                Draw();
        }
    }

I did manage to get a SFML RenderWindow embedded inside the form.
 public partial class Form1 : Form
    {
        private RenderWindow mapWindow;
        private bool running = true;

        public Form1()
        {
            InitializeComponent();
         
        }

        private void Draw()
        {
   
            Application.DoEvents();
            mapWindow.Clear(new SFML.Graphics.Color(101, 156, 239));
            mapWindow.Display();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            running = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            mapWindow = new RenderWindow(this.Handle);
            while (running)
                Draw();
        }

    }
Thanks
Title: Re: Embed SFML Window in picturebox
Post by: Laurent on August 01, 2013, 08:00:06 am
I don't know if this is possible.

What happens exactly?
Title: Re: Embed SFML Window in picturebox
Post by: nickkorta on August 01, 2013, 04:17:07 pm
I see the SFML Window quickly appear and disappear. It does this when I embed the window to the form as well. So I know something is happening. The picture box does not change at all. It stays on its default settings.

Someone managed to do it here.
http://en.sfml-dev.org/forums/index.php?topic=9178.0

Thanks for the reply.
Title: Re: Embed SFML Window in picturebox
Post by: zsbzsb on August 02, 2013, 12:43:21 am
Here is a sample that works. Note you do not need your own control as I did in this example, but doing it this way prevents SFML and the control fighting to paint the same area. I commented the code so it should be self explanatory. I also recommend that you add SFML to a form from outside the form's load events as I did here.

    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);
        }
    }
Title: Re: Embed SFML Window in picturebox
Post by: nickkorta on August 02, 2013, 02:44:02 am
Thanks it works!!!