.net winform程序下使用firefox作为Web浏览器

在winform程序中,要在程序中展示一个web页面,最常用的就是.net自带的webbrowser,但是大家都知道它是IE,也知道IE是有多么强(er)大(bi)。而且微软已经宣布了IE的死亡。。。默哀1秒钟,继续正文

 

那么如何使用firefox呢?只需两步:

1、添加引用:Geckofx-Core、Geckofx-Winforms;图1

2、把xulrunner放在你项目的bin目录下;图2

  图1

  图2

上面两个资源的下载地址:https://bitbucket.org/geckofx/geckofx-33.0/downloads

ftp://ftp.mozilla.org/pub/xulrunner/releases/33.0/runtimes/

但是要注意一点:geckofx的版本要和xulrunner的版本对应,不然会有问题。xulrunner是firefox官网提供的,有最新版本,但是geckofx版本相对落后一些,但是也更新到了33.0版本,对应firefox的发布时间是2014年10月份,应该也够用了。

 

当把前面的资源配置好后,接下来就是调用api的活了,非常简单,上一段简单的代码。

新建一个winform程序,然后在默认的form1.cs文件中加入以下代码。

 1     public partial class Form1 : Form
 2     {
 3         private readonly string xulrunnerPath = Application.StartupPath + "/xulrunner";
 4         private const string testUrl = "https://www.alipay.com/";  
 5         private GeckoWebBrowser Browser; 
 6         public Form1()
 7         {
 8             InitializeComponent();
 9 
10             Xpcom.Initialize(xulrunnerPath);
11 
12             
13         }
14 
15         private void Form1_Load(object sender, EventArgs e)
16         {
17             Browser = new GeckoWebBrowser();
18             Browser.Parent = this;
19             Browser.Dock = DockStyle.Fill;
20             Browser.Navigate(testUrl);  
21         }
22     }

OK,运行项目你将会看到支付宝的首页。

posted on 2015-05-12 16:23  橙子瓣  阅读(10849)  评论(7编辑  收藏  举报