Search code examples
c#htmlvideofullscreencefsharp

CefSharp OnFullscreenModeChange event


I'm currently working on a CefSharp project, and I'm currently working on getting HTML5 videos to open in fullscreen, inside my application.

I've searched the internet for answers about how this kind of event works, which I don't quite understand yet.

I my winforms project I've gotten this far:

public partial class Form1 : Form
{
    public ChromiumWebBrowser chromeBrowser;

    public Form1()
    {
        InitializeComponent();
        InitializeChromium();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void InitializeChromium()
    {
        CefSettings settings = new CefSettings();

        Cef.Initialize(settings);

        chromeBrowser = new ChromiumWebBrowser("http://youtube.com/");

        this.Controls.Add(chromeBrowser);
        chromeBrowser.Dock = DockStyle.Fill;
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        Cef.Shutdown();
    }
}

But every time I load a youtube video (or a video from a different website) and hit on a "Fullscreen" button, the video only fills the application, and not the entire monitor screen. I guess this is due to the fact that I havn't implemented the OnFullscreenModeChange method yet, from the IDisplayHandler interface.

In my search for answers I've seen that I can use a method to overload CefSharp's IDisplayHandler.OnFullscreenModeChange method, with something like this:

class DisplayHandler : IDisplayHandler
{
...
    void IDisplayHandler.OnFullscreenModeChange(IWebBrowser browserControl, IBrowser browser, bool fullscreen)
    {
        var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

        chromiumWebBrowser.InvokeOnUiThreadIfRequired(() =>
        {
            if (fullscreen)
            {
                parent = chromiumWebBrowser.Parent;

                parent.Controls.Remove(chromiumWebBrowser);

                fullScreenForm = new Form();
                fullScreenForm.FormBorderStyle = FormBorderStyle.None;
                fullScreenForm.WindowState = FormWindowState.Maximized;

                fullScreenForm.Controls.Add(chromiumWebBrowser);

                fullScreenForm.ShowDialog(parent.FindForm());
            }
            else
            {
                fullScreenForm.Controls.Remove(chromiumWebBrowser);

                parent.Controls.Add(chromiumWebBrowser);

                fullScreenForm.Close();
                fullScreenForm.Dispose();
                fullScreenForm = null;
            }
        });
    }
...
}

So my question is: How do I add this overload to my ChromiumWebBrowser control?


Solution

  • I've solved the issue by using GeckoFX web browser instead of using CefSharp.

    I've choosen to use version 33 of GeckoFX due to a issue with the FullScreen API in version 45.

    My solution:

    ...
    Xpcom.Initialize(@"..\..\xulrunner");
    
    GeckoPreferences.User["full-screen-api.enabled"] = true;
    GeckoPreferences.Default["full-screen-api.enabled"] = true;
    
    geckoWebBrowser1.EnableDefaultFullscreen();
    
    geckoWebBrowser1.Navigate(url);
    
    geckoWebBrowser1.GetDocShellAttribute().SetIsBrowserInsideApp(0);
    geckoWebBrowser1.GetDocShellAttribute().SetFullscreenAllowed(true);
    ...