Chinaunix首页 | 论坛 | 博客
  • 博客访问: 249629
  • 博文数量: 65
  • 博客积分: 2758
  • 博客等级: 少校
  • 技术积分: 725
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-25 00:23
文章分类

全部博文(65)

文章存档

2011年(4)

2010年(1)

2009年(60)

我的朋友

分类: 嵌入式

2011-02-27 13:43:42

进行webBrower开发的时候,肯定都会遇到一个问题。
那就是怎么样强制在本窗口打开新窗口的问题。
网上最常见的解决方法就是,
在NewWindow事件中取得要打开的网址,取消打开新窗口,然后在本窗口打开要转向的网址

  1. p rivate void webBrowser_1_NewWindow(object sender, CancelEventArgs e)
  2. {
  3. WebBrowser webBrowser_temp = (WebBrowser)sender;
  4. string newUrl = webBrowser_temp.Document.ActiveElement.GetAttribute("href");
  5. webBrowser_1.Url = new Uri(newUrl);
  6. e.Cancel = true;
  7. }

  虽然这种方法能够解决大部分的要求,但是治标不治本。
而且当网址里面还有汉字的时候就更加麻烦了,比如在百度MP3,歌曲名字都是汉字,和百度空间里面,大部分用户名都是汉字,所以取的网址都用乱码。虽然,可以对网址进行编码,但是并不是所有网页都是uft-8编码,对于如何得知网页的编码又是一个课题了。

最根本的方法就是重写了。
新建一个类

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;

  4. namespace webTestRecorder
  5. {

  6.     public class ExtendedWebBrowser : System.Windows.Forms.WebBrowser
  7.     {
  8.         System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
  9.         WebBrowserExtendedEvents events;

  10.         //This method will be called to give you a chance to create your own event sink

  11.         protected override void CreateSink()
  12.         {
  13.             //MAKE SURE TO CALL THE BASE or the normal events won't fire

  14.             base.CreateSink();
  15.             events = new WebBrowserExtendedEvents(this);
  16.             cookie = new System.Windows.Forms.AxHost.ConnectionPointCookie(this.ActiveXInstance, events, typeof(DWebBrowserEvents2));
  17.         }

  18.         protected override void DetachSink()
  19.         {
  20.             if (null != cookie)
  21.             {
  22.                 cookie.Disconnect();
  23.                 cookie = null;
  24.             }
  25.             base.DetachSink();
  26.         }

  27.         //This new event will fire when the page is navigating

  28.         public event EventHandler<WebBrowserExtendedNavigatingEventArgs> BeforeNavigate;
  29.         public event EventHandler<WebBrowserExtendedNavigatingEventArgs> BeforeNewWindow;

  30.         protected void OnBeforeNewWindow(string url, out bool cancel)
  31.         {
  32.             EventHandler<WebBrowserExtendedNavigatingEventArgs> h = BeforeNewWindow;
  33.             WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, null);
  34.             if (null != h)
  35.             {
  36.                 h(this, args);
  37.             }
  38.             cancel = args.Cancel;
  39.         }

  40.         protected void OnBeforeNavigate(string url, string frame, out bool cancel)
  41.         {
  42.             EventHandler<WebBrowserExtendedNavigatingEventArgs> h = BeforeNavigate;
  43.             WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, frame);
  44.             if (null != h)
  45.             {
  46.                 h(this, args);
  47.             }
  48.             //Pass the cancellation chosen back out to the events

  49.             cancel = args.Cancel;
  50.         }
  51.         //This class will capture events from the WebBrowser

  52.         class WebBrowserExtendedEvents : System.Runtime.InteropServices.StandardOleMarshalObject, DWebBrowserEvents2
  53.         {
  54.             ExtendedWebBrowser _Browser;
  55.             public WebBrowserExtendedEvents(ExtendedWebBrowser browser) { _Browser = browser; }

  56.             //Implement whichever events you wish

  57.             public void BeforeNavigate2(object pDisp, ref object URL, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
  58.             {
  59.                 _Browser.OnBeforeNavigate((string)URL, (string)targetFrameName, out cancel);
  60.             }

  61.             public void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL)
  62.             {
  63.                 _Browser.OnBeforeNewWindow((string)URL, out cancel);
  64.             }

  65.         }
  66.         [System.Runtime.InteropServices.ComImport(), System.Runtime.InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
  67.         System.Runtime.InteropServices.InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch),
  68.         System.Runtime.InteropServices.TypeLibType(System.Runtime.InteropServices.TypeLibTypeFlags.FHidden)]
  69.         public interface DWebBrowserEvents2
  70.         {

  71.             [System.Runtime.InteropServices.DispId(250)]
  72.             void BeforeNavigate2(
  73.                 [System.Runtime.InteropServices.In,
  74.                 System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
  75.                 [System.Runtime.InteropServices.In] ref object URL,
  76.                 [System.Runtime.InteropServices.In] ref object flags,
  77.                 [System.Runtime.InteropServices.In] ref object targetFrameName, [System.Runtime.InteropServices.In] ref object postData,
  78.                 [System.Runtime.InteropServices.In] ref object headers,
  79.                 [System.Runtime.InteropServices.In,
  80.                 System.Runtime.InteropServices.Out] ref bool cancel);
  81.             [System.Runtime.InteropServices.DispId(273)]
  82.             void NewWindow3(
  83.                 [System.Runtime.InteropServices.In,
  84.                 System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
  85.                 [System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] ref bool cancel,
  86.                 [System.Runtime.InteropServices.In] ref object flags,
  87.                 [System.Runtime.InteropServices.In] ref object URLContext,
  88.                 [System.Runtime.InteropServices.In] ref object URL);

  89.         }
  90.     }

  91.     public class WebBrowserExtendedNavigatingEventArgs : System.ComponentModel.CancelEventArgs
  92.     {
  93.         p rivate string _Url;
  94.         public string Url
  95.         {
  96.             get { return _Url; }
  97.         }

  98.         p rivate string _Frame;
  99.         public string Frame
  100.         {
  101.             get { return _Frame; }
  102.         }

  103.         public WebBrowserExtendedNavigatingEventArgs(string url, string frame)
  104.             : base()
  105.         {
  106.             _Url = url;
  107.             _Frame = frame;
  108.         }
  109.     }
  110. }

然后把webBrowser换成我们重写的ExtendedWebBrowser,添加事件处理

  1. ieBrowser = new ExtendedWebBrowser();
  2. ieBrowser.BeforeNewWindow += new EventHandler<WebBrowserExtendedNavigatingEventArgs>(ieBrowser_BeforeNewWindow);

 

然后在其BeforeNewWindow事件中:

  1. void ieBrowser_BeforeNewWindow(object sender, WebBrowserExtendedNavigatingEventArgs e) {
  2.      e.Cancel=true;
  3.      ((ExtendedWebBrowser)sender).Navigate(e.Url);
  4. }

---------------------另一方案下下----------------------

 

要解决这个问题,可以使用下面的方法:

在日常的开发中,大家有时需要用WebBrowser加载URL,来实现某些功能。而这时,我们就不希望所打开的页面中的链接,在新窗口中打开,因为这样的话,实际上是用系统默认的浏览器打开了,从而脱离了你的WebBrowser,也就不能被你所控制了。

假设WebBrowser的Name是 webBrowser1

 

  1. ******* void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

  2. { //将所有的链接的目标,指向本窗体


  3. foreach (HtmlElement archor in this.webBrowser1.Document.Links)

  4. {

  5. archor.SetAttribute("target", "_self");

  6. } //将所有的FORM的提交目标,指向本窗体 foreach (HtmlElement form in this.webBrowser1.Document.Forms){ form.SetAttribute("target", "_self"); }}


  7. ******* void webBrowser1_NewWindow(object sender, CancelEventArgs e){ e.Cancel = true;}

 

记得将 WebBrowser 的 AllowWebBrowserDrop 设为 false

将 WebBrowser 的 WebBrowserShortcutsEnabled 设为 false

将 WebBrowser 的 IsWebBrowserContextMenuEnabled 设为 false

阅读(5456) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~