UMPLatForm.NET 中MiNiWeb瀏覽器核心技術詳解二(接上篇)

   UMPLatForm.NET MiNiWeb瀏覽器核心技術詳解二

 

接上一篇:UMPLatForm.NET 中MiNiWeb瀏覽器核心技術詳解一

 

3、創建webbrowser擴展組件

      從上一節中,我們可以發現上述的所有內容都基本可以歸結爲兩件事:

1.       實現一個iwebbrowser2類型的對象,從中獲得application屬性

2.       實現dwebbrowserevents2接口來觸發事件

 實現iwebbrowser2接口

  1. using System; 
  2. using System.Security; 
  3. using System.Runtime.InteropServices; 
  4. using System.Windows.Forms; 
  5. using System.Security.Permissions; 
  6.  
  7. namespace MiniBrowser 
  8.     /// <summary> 
  9.     /// An extended version of the <see cref="WebBrowser"/> control. 
  10.     /// </summary> 
  11.     class ExtendedWebBrowser : System.Windows.Forms.WebBrowser 
  12.     { 
  13.         private UnsafeNativeMethods.IWebBrowser2 axIWebBrowser2; 
  14.  
  15.         /// <summary> 
  16.         /// This method supports the .NET Framework infrastructure and is not intended to be used directly from your code. 
  17.         /// Called by the control when the underlying ActiveX control is created. 
  18.         /// </summary> 
  19.         /// <param name="nativeActiveXObject"></param> 
  20.         [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")] 
  21.         protected override void AttachInterfaces(objectnativeActiveXObject) 
  22.         { 
  23.             this.axIWebBrowser2 = (UnsafeNativeMethods.IWebBrowser2)nativeActiveXObject; 
  24.             base.AttachInterfaces(nativeActiveXObject); 
  25.         } 
  26.  
  27.         /// <summary> 
  28.         /// This method supports the .NET Framework infrastructure and is not intended to be used directly from your code. 
  29.         /// Called by the control when the underlying ActiveX control is discarded. 
  30.         /// </summary> 
  31.         [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")] 
  32.         protected override void DetachInterfaces() 
  33.         { 
  34.             this.axIWebBrowser2 = null
  35.             base.DetachInterfaces(); 
  36.         } 
  37.  
  38.         /// <summary> 
  39.         /// Returns the automation object for the web browser 
  40.         /// </summary> 
  41.         public object Application 
  42.         { 
  43.             get { return axIWebBrowser2.Application; } 
  44.         } 
  45.  
  46.         System.Windows.Forms.AxHost.ConnectionPointCookie cookie; 
  47.         WebBrowserExtendedEvents events; 
  48.  
  49.         /// <summary> 
  50.         /// This method will be called to give you a chance to create your own event sink 
  51.         /// </summary> 
  52.         [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")] 
  53.         protected override void CreateSink() 
  54.         { 
  55.             // Make sure to call the base class or the normal events won't fire 
  56.             base.CreateSink(); 
  57.             events = new WebBrowserExtendedEvents(this); 
  58.             cookie = newAxHost.ConnectionPointCookie(this.ActiveXInstance, events,typeof(UnsafeNativeMethods.DWebBrowserEvents2)); 
  59.         } 
  60.  
  61.         /// <summary> 
  62.         /// Detaches the event sink 
  63.         /// </summary> 
  64.         [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")] 
  65.         protected override void DetachSink() 
  66.         { 
  67.             if (null != cookie) 
  68.             { 
  69.                 cookie.Disconnect(); 
  70.                 cookie = null
  71.             } 
  72.         } 
  73.  
  74.         /// <summary> 
  75.         /// Fires when downloading of a document begins 
  76.         /// </summary> 
  77.         public event EventHandler Downloading; 
  78.  
  79.         /// <summary> 
  80.         /// Raises the <see cref="Downloading"/> event 
  81.         /// </summary> 
  82.         /// <param name="e">Empty <see cref="EventArgs"/></param> 
  83.         /// <remarks> 
  84.         /// You could start an animation or a notification that downloading is starting 
  85.         /// </remarks> 
  86.         protected void OnDownloading(EventArgs e) 
  87.         { 
  88.             if (Downloading != null
  89.                 Downloading(this, e); 
  90.         } 
  91.  
  92.         /// <summary> 
  93.         /// Fires when downloading is completed 
  94.         /// </summary> 
  95.         /// <remarks> 
  96.         /// Here you could start monitoring for script errors. 
  97.         /// </remarks> 
  98.         public event EventHandler DownloadComplete; 
  99.  
  100.         /// <summary> 
  101.         /// Raises the <see cref="DownloadComplete"/> event 
  102.         /// </summary> 
  103.         /// <param name="e">Empty <see cref="EventArgs"/></param> 
  104.         protected virtual void OnDownloadComplete(EventArgs e) 
  105.         { 
  106.             if (DownloadComplete != null
  107.                 DownloadComplete(this, e); 
  108.         } 
  109.        …… 
  110.     } 


webbrowser控件有兩個尚未公開的接口:attachinterfaces()detachinterfaces()。這些方法用於獲得iwebbrowser2接口的引用。 

下一步,我們可以添加application屬性。

    

  1. /// <summary> 
  2. /// Returns the automation object for the web browser 
  3. /// </summary> 
  4. public object Application 
  5.     get { return axIWebBrowser2.Application; } 

       這個屬性可以用來創建一個新窗口,並且當創建新窗口事件觸發時將瀏覽器重定向到這個新窗口。  

 



作者: Edward  
出處: 
http://umplatform.blog.51cto.com/

微博: 騰訊

Email: [email protected]或 [email protected]

QQ 交流:406590790 (請註明:平臺交流)

QQ羣交流:16653241  237326100
關於作者:高級工程師、信息系統項目管理師、數據庫系統工程師。專注於微軟平臺項目架構、管理和企業解決方案,多年項目開發與管理經驗,曾多次組織並開發多個大型項目,精通DotNet(C#Asp.NETADO.NETWeb ServiceWCF)DB原理與技術、SqlServerOracle等。熟悉JavaDelhpiLinux操作系統,有紮實的網絡知識。自認在面向對象、面向服務以及數據庫領域有一定的造詣。現主要從事DB管理、DB開發、WinFormWCFWebService、網頁數據抓取以及ASP.NET等項目管理、開發、架構等工作。如有問題或建議,請多多賜教!
本文版權歸作者和51CTO博客共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,如有問題,可以通過郵箱或QQ
 聯繫我,非常感謝。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章