進行webBrower開發的時候,肯定都會遇到一個問題。
那就是怎麼樣強制在本窗口打開新窗口的問題。
網上最常見的解決方法就是,
在NewWindow事件中取得要打開的網址,取消打開新窗口,然後在本窗口打開要轉向的網址
private void webBrowser_1_NewWindow(object sender, CancelEventArgs e)
{
WebBrowser webBrowser_temp = (WebBrowser)sender;
string newUrl = webBrowser_temp.Document.ActiveElement.GetAttribute("href");
webBrowser_1.Url = new Uri(newUrl);
e.Cancel = true;
}
雖然這種方法能夠解決大部分的要求,但是治標不治本。
而且當網址裡面還有漢字的時候就更加麻煩了,比如在百度MP3,歌曲名字都是漢字,和百度空間裡面,大部分用戶名都是漢字,所以取的網址都用亂碼。雖然,可以對網址進行編碼,但是並不是所有網頁都是uft-8編碼,對於如何得知網頁的編碼又是一個課題了。
最根本的方法就是重寫了。
新建一個類
codeusing System;
using System.Collections.Generic;
using System.Text;
namespace webTestRecorder
{
public class ExtendedWebBrowser : System.Windows.Forms.WebBrowser
{
System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
WebBrowserExtendedEvents events;
//This method will be called to give you a chance to create your own event sink
protected override void CreateSink()
{
//MAKE SURE TO CALL THE BASE or the normal events won't fire
base.CreateSink();
events = new WebBrowserExtendedEvents(this);
cookie = new System.Windows.Forms.AxHost.ConnectionPointCookie(this.ActiveXInstance, events, typeof(DWebBrowserEvents2));
}
protected override void DetachSink()
{
if (null != cookie)
{
cookie.Disconnect();
cookie = null;
}
base.DetachSink();
}
//This new event will fire when the page is navigating
public event EventHandler BeforeNavigate;
public event EventHandler BeforeNewWindow;
protected void OnBeforeNewWindow(string url, out bool cancel)
{
EventHandler h = BeforeNewWindow;
WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, null);
if (null != h)
{
h(this, args);
}
cancel = args.Cancel;
}
protected void OnBeforeNavigate(string url, string frame, out bool cancel)
{
EventHandler h = BeforeNavigate;
WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, frame);
if (null != h)
{
h(this, args);
}
//Pass the cancellation chosen back out to the events
cancel = args.Cancel;
}
//This class will capture events from the WebBrowser
class WebBrowserExtendedEvents : System.Runtime.InteropServices.StandardOleMarshalObject, DWebBrowserEvents2
{
ExtendedWebBrowser _Browser;
public WebBrowserExtendedEvents(ExtendedWebBrowser browser) { _Browser = browser; }
//Implement whichever events you wish
public void BeforeNavigate2(object pDisp, ref object URL, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
{
_Browser.OnBeforeNavigate((string)URL, (string)targetFrameName, out cancel);
}
public void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL)
{
_Browser.OnBeforeNewWindow((string)URL, out cancel);
}
}
[System.Runtime.InteropServices.ComImport(), System.Runtime.InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
System.Runtime.InteropServices.InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch),
System.Runtime.InteropServices.TypeLibType(System.Runtime.InteropServices.TypeLibTypeFlags.FHidden)]
public interface DWebBrowserEvents2
{
[System.Runtime.InteropServices.DispId(250)]
void BeforeNavigate2(
[System.Runtime.InteropServices.In,
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
[System.Runtime.InteropServices.In] ref object URL,
[System.Runtime.InteropServices.In] ref object flags,
[System.Runtime.InteropServices.In] ref object targetFrameName, [System.Runtime.InteropServices.In] ref object postData,
[System.Runtime.InteropServices.In] ref object headers,
[System.Runtime.InteropServices.In,
System.Runtime.InteropServices.Out] ref bool cancel);