How to add fullscreen support to a GeckoFx browser?

好久不见. 提交于 2021-01-05 11:58:46

问题


I'm building a web browser in a C# .NET Windows Form Application and wanted to add support for fullscreen usage (mainly on HTML5 videos).

So that when users press the fullscreen button on a video like a youtube video, the video would take up the full screen.

The browser uses a GeckoFx control to view the interweb. How would I go about doing this?


回答1:


Set the Browser Control Dock property to Dock.Fill.
You can get the Screen size where you program is currently displayed and use its Bounds to size your Form.

Subscribe to the Resize event. When the Form is maximized, remove the border (which will remove the title bar, too), set the FormWindowState back to FormWindowState.Normal (otherwise you won't be able to use the full screen size) then resize it as needed.

You application should be DPIAware (if it's not see here).
You should also handle the F11 key to allow the user to maximize/normalize the Form's window.

using System;
using System.Drawing;
using System.Windows.Forms;
using Gecko;
using Screen = System.Windows.Forms.Screen;

public partial class Form1 : Form
{
    bool IsMaximized = false;
    bool TheaterClicked = false;
    Rectangle previousPosition = Rectangle.Empty;
    string UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0";
    public Form1()
    {
        InitializeComponent();
        Xpcom.Initialize("Firefox");
        GeckoPreferences.User["full-screen-api.enabled"] = true;
        GeckoPreferences.Default["full-screen-api.enabled"] = true;
        GeckoPreferences.User["general.useragent.override"] = UserAgent;
        GeckoPreferences.Default["general.useragent.override"] = UserAgent;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        previousPosition = this.Bounds;
        this.geckoWebBrowser1.Navigate("[Some URL]");
        this.geckoWebBrowser1.GetDocShellAttribute().SetFullscreenAllowed(true);
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Maximized) { 
            SetWindowState(this.WindowState, false);
        }
        else if (!this.IsMaximized) { 
            this.previousPosition = this.Bounds;
        }
    }

    private void geckoWebBrowser1_DomMouseDown(object sender, DomMouseEventArgs e)
    {
        if (geckoWebBrowser1.Url.Host.Contains("youtu"))
        {
            GeckoHtmlElement elm = (GeckoHtmlElement)e.Target.CastToGeckoElement();
            switch (elm.ClassName)
            {
                case "ytp-fullscreen-button ytp-button":
                    if (this.geckoWebBrowser1.Document.GetElementsByClassName("ytp-size-button ytp-button").FirstOrDefault() is GeckoHtmlElement theater)
                    {
                        if (this.TheaterClicked == false) {
                            theater.Click();
                            this.TheaterClicked = true;
                        }
                    }
                    break;
                case "ytp-size-button ytp-button":
                    this.TheaterClicked = !this.TheaterClicked;
                    break;
                default:
                    break;
            }
        }
    }

    private void SetWindowState(FormWindowState state, bool setSize)
    {
        if (state == FormWindowState.Maximized) {
            this.IsMaximized = true;
            if (setSize) this.previousPosition = this.Bounds;
            this.WindowState = FormWindowState.Normal;
            this.FormBorderStyle = FormBorderStyle.None;
            this.Location = Point.Empty;
            this.Size = Screen.FromHandle(this.Handle).Bounds.Size;
        }
        else {
            this.FormBorderStyle = FormBorderStyle.Sizable;
            this.Bounds = this.previousPosition;
            this.IsMaximized = false;
        }
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        base.ProcessCmdKey(ref msg, keyData);
        if (keyData == Keys.F11) {
            SetWindowState(this.IsMaximized ? FormWindowState.Normal : FormWindowState.Maximized, true);
            return true;
        }
        else {
            return false;
        }
    }
}


来源:https://stackoverflow.com/questions/54508558/how-to-add-fullscreen-support-to-a-geckofx-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!