浏览器包装器 ( Mozilla IE )






3.35/5 (7投票s)
2007年9月2日
2分钟阅读

98422

3644
描述了一个用于使用 mozilla 或 IE 渲染 HTML 页面的包装器。

引言
如果你想在你的应用程序中嵌入一个网页浏览器,使用 dotnet 2.0,有一个新的控件 WebBrowser
(一篇关于 WebBrowser 的好文章) 。它是 Internet Explorer 的引擎。
我最近发现有一个 Mozilla ActiveX 插件,所以我尝试制作一个 C# 封装类,以便可以选择使用 Internet Explorer 或 Mozilla 来渲染 HTML 页面。
本文将展示如何使用 Mozilla ActiveX,以及如何同时使用 IE 和 Mozilla。
我不会深入研究 Mozilla Active X,也许会在本文的后续更新中进行。
我制作了一个小型的测试应用程序,其中包含两个 HTML 渲染对象。
我截取了两张截图
一张使用 IE,另一张使用 Mozilla。
要区分这两个浏览器,请查看上下文菜单。
Internet Explorer
Mozilla
安装 Mozilla ActiveX
安装 Mozilla ActiveX 插件。点击工具箱中的右键,选择“项”。
你会在 Com 对象中找到 MozillaBrowser 类,并用红色下划线标出。


因此,当你选中它时,工具箱中会出现一个新的图标
使用代码
类 WrapperWebBrowser.cs
包含
// The constructor public WrapperWebBrowser( ref AxMOZILLACONTROLLib.AxMozillaBrowser moz, ref System.Windows.Forms.WebBrowser ie ) public enum EnumTypeBrowser { IE, MOZILLA }; // You can set , or get the TypeBrowser // Exemple TypeBrowser = EnumTypeBrowser.IE public EnumTypeBrowser TypeBrowser; // Set the delegate when the document complete downloaded public void SetDocumentCompleteEvent(DelegateDocumentComplete docCompleteEvent) // Set the url of the browser web. public void Navigate(string url)
如果你想使用 WrapperWebBrowser.cs
,你需要创建一个
WebBrowser
和 axMozillaBrowser
WrapperWebBrowser wrapperWebBrowser = null; public Form1() { InitializeComponent(); wrapperWebBrowser = new WrapperWebBrowser(ref this.axMozillaBrowser1,ref this.webBrowser1); wrapperWebBrowser.SetDocumentCompleteEvent(DocumentComplete); } private void DocumentComplete(string url) { this.Text = url; }
如果你更改了 wraperWebBrowser.TypeBrowser 属性,那么你选择的浏览器的窗口将会可见,
而另一个浏览器的窗口当然将不可见。
关注点
WebBrowser 和 AxMozillaBrowser 之间存在一些差异,但它们非常相似。
两者都有 Navigate,但 Document 完成时的回调名称略有不同
mozillaBrowser.DocumentComplete;
IEBrowser.DocumentCompleted;
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
private void axMozillaBrowser_DocumentComplete(object sender, AxMOZILLACONTROLLib.DWebBrowserEvents2_DocumentCompleteEvent e)