您好,登錄后才能下訂單哦!
UMPLatForm.NET 中MiNiWeb瀏覽器核心技術詳解二
接上一篇:UMPLatForm.NET 中MiNiWeb瀏覽器核心技術詳解一
3、創建webbrowser擴展組件
從上一節中,我們可以發現上述的所有內容都基本可以歸結為兩件事:
1. 實現一個iwebbrowser2類型的對象,從中獲得application屬性
2. 實現dwebbrowserevents2接口來觸發事件
實現iwebbrowser2接口
- using System;
- using System.Security;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- using System.Security.Permissions;
- namespace MiniBrowser
- {
- /// <summary>
- /// An extended version of the <see cref="WebBrowser"/> control.
- /// </summary>
- class ExtendedWebBrowser : System.Windows.Forms.WebBrowser
- {
- private UnsafeNativeMethods.IWebBrowser2 axIWebBrowser2;
- /// <summary>
- /// This method supports the .NET Framework infrastructure and is not intended to be used directly from your code.
- /// Called by the control when the underlying ActiveX control is created.
- /// </summary>
- /// <param name="nativeActiveXObject"></param>
- [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
- protected override void AttachInterfaces(objectnativeActiveXObject)
- {
- this.axIWebBrowser2 = (UnsafeNativeMethods.IWebBrowser2)nativeActiveXObject;
- base.AttachInterfaces(nativeActiveXObject);
- }
- /// <summary>
- /// This method supports the .NET Framework infrastructure and is not intended to be used directly from your code.
- /// Called by the control when the underlying ActiveX control is discarded.
- /// </summary>
- [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
- protected override void DetachInterfaces()
- {
- this.axIWebBrowser2 = null;
- base.DetachInterfaces();
- }
- /// <summary>
- /// Returns the automation object for the web browser
- /// </summary>
- public object Application
- {
- get { return axIWebBrowser2.Application; }
- }
- System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
- WebBrowserExtendedEvents events;
- /// <summary>
- /// This method will be called to give you a chance to create your own event sink
- /// </summary>
- [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
- protected override void CreateSink()
- {
- // Make sure to call the base class or the normal events won't fire
- base.CreateSink();
- events = new WebBrowserExtendedEvents(this);
- cookie = newAxHost.ConnectionPointCookie(this.ActiveXInstance, events,typeof(UnsafeNativeMethods.DWebBrowserEvents2));
- }
- /// <summary>
- /// Detaches the event sink
- /// </summary>
- [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
- protected override void DetachSink()
- {
- if (null != cookie)
- {
- cookie.Disconnect();
- cookie = null;
- }
- }
- /// <summary>
- /// Fires when downloading of a document begins
- /// </summary>
- public event EventHandler Downloading;
- /// <summary>
- /// Raises the <see cref="Downloading"/> event
- /// </summary>
- /// <param name="e">Empty <see cref="EventArgs"/></param>
- /// <remarks>
- /// You could start an animation or a notification that downloading is starting
- /// </remarks>
- protected void OnDownloading(EventArgs e)
- {
- if (Downloading != null)
- Downloading(this, e);
- }
- /// <summary>
- /// Fires when downloading is completed
- /// </summary>
- /// <remarks>
- /// Here you could start monitoring for script errors.
- /// </remarks>
- public event EventHandler DownloadComplete;
- /// <summary>
- /// Raises the <see cref="DownloadComplete"/> event
- /// </summary>
- /// <param name="e">Empty <see cref="EventArgs"/></param>
- protected virtual void OnDownloadComplete(EventArgs e)
- {
- if (DownloadComplete != null)
- DownloadComplete(this, e);
- }
- ……
- }
- }
webbrowser控件有兩個尚未公開的接口:attachinterfaces()和detachinterfaces()。這些方法用于獲得iwebbrowser2接口的引用。
下一步,我們可以添加application屬性。
- /// <summary>
- /// Returns the automation object for the web browser
- /// </summary>
- public object Application
- {
- get { return axIWebBrowser2.Application; }
- }
這個屬性可以用來創建一個新窗口,并且當創建新窗口事件觸發時將瀏覽器重定向到這個新窗口。
作者: Edward
出處: http://umplatform.blog.51cto.com/
微博: 騰訊
Email: umplatform@126.com或 406590790@qq.com
QQ 交流:406590790 (請注明:平臺交流)
QQ×××流:16653241 或 237326100
關于作者:高級工程師、信息系統項目管理師、數據庫系統工程師。專注于微軟平臺項目架構、管理和企業解決方案,多年項目開發與管理經驗,曾多次組織并開發多個大型項目,精通DotNet(C#、Asp.NET、ADO.NET、Web Service、WCF),DB原理與技術、SqlServer、Oracle等。熟悉Java、Delhpi及Linux操作系統,有扎實的網絡知識。自認在面向對象、面向服務以及數據庫領域有一定的造詣。現主要從事DB管理、DB開發、WinForm、WCF、WebService、網頁數據抓取以及ASP.NET等項目管理、開發、架構等工作。如有問題或建議,請多多賜教!
本文版權歸作者和51CTO博客共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,如有問題,可以通過郵箱或QQ 聯系我,非常感謝。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。