RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
UMPLatForm.NET中MiNiWeb浏览器核心技术详解二(接上篇)

   UMPLatForm.NET 中MiNiWeb浏览器核心技术详解二

成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站设计、做网站、定州网络推广、微信小程序、定州网络营销、定州企业策划、定州品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供定州建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com

 

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


webbrowser控件有两个尚未公开的接口:attachinterfaces()和detachinterfaces()。这些方法用于获得iwebbrowser2接口的引用。 

下一步,我们可以添加application属性。

    

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

       这个属性可以用来创建一个新窗口,并且当创建新窗口事件触发时将浏览器重定向到这个新窗口。  

 



作者: 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 联系我,非常感谢。

 


名称栏目:UMPLatForm.NET中MiNiWeb浏览器核心技术详解二(接上篇)
本文来源:http://dtcuyxr.com/article/ghijgi.html