.net 内嵌 GeckoWebBrowser (firefox) 核心浏览器
阅读原文时间:2023年07月15日阅读:1

引用nuget包:

注意:Geckofx45 nuget包必须是最后引用,否则初始化会出错

简单示例:

using Gecko;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
GeckoWebBrowser gecko;
public Form1()
{
InitializeComponent();

        Xpcom.Initialize("Firefox");

        gecko = new GeckoWebBrowser();  
        gecko.CreateControl();

        gecko.NoDefaultContextMenu = true; //禁用右键菜单

        gecko.Dock = DockStyle.Fill;  
        panel1.Controls.Add(gecko);  
        gecko.ProgressChanged += Gecko\_ProgressChanged;  
        gecko.CreateWindow += Gecko\_CreateWindow;  
        gecko.DocumentCompleted += Gecko\_DocumentCompleted;  
        gecko.Navigate("http://www.baidu.com");

    }

    private void Gecko\_DocumentCompleted(object sender, Gecko.Events.GeckoDocumentCompletedEventArgs e)  
    {  
        //var executor = new Gecko.JQuery.JQueryExecutor(gecko.Window);  //先获取到jquery对象

        //executor.ExecuteJQuery("$('#a')");    //然后执行jquery的代码  
        using (AutoJSContext context = new AutoJSContext(gecko.Window))  
        {  
            string result;  
            context.EvaluateScript("3 + 2;", out result);  
            context.EvaluateScript("'hello' + ' ' + 'world';", out result);  
        }

        progressBar1.Value = ;  
    }

    private void Gecko\_CreateWindow(object sender, GeckoCreateWindowEventArgs e)  
    {  
        e.InitialHeight = ;  
        e.InitialWidth = ;  
    }

    private void Gecko\_ProgressChanged(object sender, GeckoProgressEventArgs e)  
    {  
        if (e.MaximumProgress ==  )  
            return;

        var value = (int)Math.Min(, (e.CurrentProgress \* ) / e.MaximumProgress);  
        if (value == )  
            return;  
        progressBar1.Value = value;  
    }  
}  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器