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

Author Topic: Embed SFML Window in picturebox(Answered)  (Read 6592 times)

0 Members and 1 Guest are viewing this topic.

nickkorta

  • Newbie
  • *
  • Posts: 17
    • View Profile
Embed SFML Window in picturebox(Answered)
« 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
« Last Edit: August 02, 2013, 02:44:40 am by nickkorta »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Embed SFML Window in picturebox
« Reply #1 on: August 01, 2013, 08:00:06 am »
I don't know if this is possible.

What happens exactly?
Laurent Gomila - SFML developer

nickkorta

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Embed SFML Window in picturebox
« Reply #2 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.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Embed SFML Window in picturebox
« Reply #3 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);
        }
    }
« Last Edit: January 19, 2014, 06:19:39 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

nickkorta

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Embed SFML Window in picturebox
« Reply #4 on: August 02, 2013, 02:44:02 am »
Thanks it works!!!