using DotNetBrowser;
using DotNetBrowser.WinForms;

using System.Windows.Forms;
using System;

namespace WinFormsFullScreenHandlerSample
{
    public partial class Form1 : Form
    {
        private BrowserView browserView;
        public Form1()
        {
            InitializeComponent();
            browserView = new WinFormsBrowserView(BrowserFactory.Create(BrowserType.LIGHTWEIGHT));
            Controls.Add((Control)browserView);
            browserView.Browser.FullScreenHandler = new SampleFullScreenHandler(this, browserView);
            browserView.Browser.LoadURL("http://www.w3.org/2010/05/video/mediaevents.html");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            browserView.Dispose();
            browserView.Browser.Dispose();
        }
    }

    internal class SampleFullScreenHandler : FullScreenHandler
    {
        private Form parent;
        private Form fullScreenForm;
        private BrowserView view;

        public SampleFullScreenHandler(Form form, BrowserView view)
        {
            this.parent = form;
            this.view = view;
        }

        public void OnFullScreenEnter()
        {
            parent.BeginInvoke((Action)(() =>
            {
                fullScreenForm = new Form();
                fullScreenForm.TopMost = true;
                fullScreenForm.ShowInTaskbar = false;
                fullScreenForm.FormBorderStyle = FormBorderStyle.None;
                fullScreenForm.WindowState = FormWindowState.Maximized;
                fullScreenForm.Owner = parent;

                fullScreenForm.Activated += delegate
                {
                    UpdateSize();
                };
                parent.Controls.Remove((Control)view);
                fullScreenForm.Controls.Add((Control)view);
                fullScreenForm.Show();
            }));
        }

        void UpdateSize()
        {
            if (fullScreenForm.Width == 0 && fullScreenForm.Height == 0)
            {
                return;
            }
            view.UpdateSize(fullScreenForm.Width, fullScreenForm.Height);
        }

        public void OnFullScreenExit()
        {
            parent.BeginInvoke((Action)(() =>
            {
                fullScreenForm.Controls.Clear();
                parent.Controls.Add((Control)view);
                fullScreenForm.Hide();
                fullScreenForm.Close();

            }));
        }
    }
}



Imports DotNetBrowser
Imports DotNetBrowser.WinForms

Public Class Form1

    Dim browserView As BrowserView

    Public Sub New()

        InitializeComponent()
        browserView = New WinFormsBrowserView(BrowserFactory.Create(BrowserType.LIGHTWEIGHT))
        Me.Controls.Add(browserView)
        browserView.Browser.FullScreenHandler = New SampleFullScreenHandler(Me, browserView)
        browserView.Browser.LoadURL("http://www.w3.org/2010/05/video/mediaevents.html")
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        browserView.Dispose()
        browserView.Browser.Dispose()
    End Sub
End Class


Friend Class SampleFullScreenHandler
    Implements FullScreenHandler

    Private parent As Form
    Private fullScreenForm As Form
    Private view As BrowserView

    Public Sub New(form As Form, view As BrowserView)
        Me.parent = form
        Me.view = view
    End Sub

    Public Sub OnFullScreenEnter() Implements FullScreenHandler.OnFullScreenEnter
        parent.BeginInvoke(
    Sub()
        fullScreenForm = New Form()
        fullScreenForm.TopMost = True
        fullScreenForm.ShowInTaskbar = False
        fullScreenForm.FormBorderStyle = FormBorderStyle.None
        fullScreenForm.WindowState = FormWindowState.Maximized
        fullScreenForm.Owner = parent

        AddHandler fullScreenForm.Activated, Sub(sender, e)
                                                 UpdateSize()
                                             End Sub

        parent.Controls.Remove(view)
        fullScreenForm.Controls.Add(view)
        fullScreenForm.Show()
    End Sub
)

    End Sub

    Sub UpdateSize()
        If ((fullScreenForm.Width = 0) And (fullScreenForm.Height = 0)) Then
            Return
        End If

        view.UpdateSize(fullScreenForm.Width, fullScreenForm.Height)

    End Sub

    Public Sub OnFullScreenExit() Implements FullScreenHandler.OnFullScreenExit
        parent.BeginInvoke(
    Sub()
        fullScreenForm.Controls.Clear()
        parent.Controls.Add(view)
        fullScreenForm.Hide()
        fullScreenForm.Close()
    End Sub
            )
    End Sub

End Class