65.9K
CodeProject 正在变化。 阅读更多。
Home

浏览器包装器 ( Mozilla IE )

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.35/5 (7投票s)

2007年9月2日

2分钟阅读

viewsIcon

98422

downloadIcon

3644

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

Screenshot - IMG76.jpg

引言

如果你想在你的应用程序中嵌入一个网页浏览器,使用 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

Screenshot - IMG77.jpg

Mozilla

Screenshot - IMG78.jpg


安装 Mozilla ActiveX

安装 Mozilla ActiveX 插件
点击工具箱中的右键,选择“项”。
你会在 Com 对象中找到 MozillaBrowser 类,并用红色下划线标出。
Screenshot - IMG79.jpg

因此,当你选中它时,工具箱中会出现一个新的图标

Screenshot - IMG82.jpg

使用代码

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,你需要创建一个 WebBrowseraxMozillaBrowser

 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)
© . All rights reserved.