此扩展类,不是本人原创,转载自:http://vsug.jp/tabid/63/forumid/45/postid/9273/view/topic/Default.aspx
Code
public class WebBrowserExtendedNavigatingEventArgs : CancelEventArgs
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string _Url;
public string Url
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Url; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string _Frame;
public string Frame
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Frame; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public WebBrowserExtendedNavigatingEventArgs(string url, string frame)
: base()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Url = url;
_Frame = frame;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class ExtendedWebBrowser : System.Windows.Forms.WebBrowser
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
WebBrowserExtendedEvents events;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//This method will be called to give you a chance to create your own event sink
protected override void CreateSink()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//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));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override void DetachSink()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (null != cookie)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cookie.Disconnect();
cookie = null;
}
base.DetachSink();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//This new event will fire when the page is navigating
public event EventHandler BeforeNavigate;
public event EventHandler BeforeNewWindow;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void OnBeforeNewWindow(string url, out bool cancel)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EventHandler h = BeforeNewWindow;
WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, null);
if (null != h)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
h(this, args);
}
cancel = args.Cancel;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void OnBeforeNavigate(string url, string frame, out bool cancel)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EventHandler h = BeforeNavigate;
WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, frame);
if (null != h)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
h(this, args);
}
//Pass the cancellation chosen back out to the events
cancel = args.Cancel;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//This class will capture events from the WebBrowser
class WebBrowserExtendedEvents : System.Runtime.InteropServices.StandardOleMarshalObject, DWebBrowserEvents2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ExtendedWebBrowser _Browser;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public WebBrowserExtendedEvents(ExtendedWebBrowser browser)
{ _Browser = browser; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Browser.OnBeforeNavigate((string)URL, (string)targetFrameName, out cancel);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Browser.OnBeforeNewWindow((string)URL, out cancel);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[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
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[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);
[System.Runtime.InteropServices.DispId(273)]
void NewWindow3(
[System.Runtime.InteropServices.In,
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
[System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] ref bool cancel,
[System.Runtime.InteropServices.In] ref object flags,
[System.Runtime.InteropServices.In] ref object URLContext,
[System.Runtime.InteropServices.In] ref object URL);
}
}
使用方法:
示例代码
ExtendedWebBrowser webBrowser;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private void webBrowser_BeforeNavigate(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//增加了这个事件,可以替代原来的NewWindow事件.
private void webBrowser_BeforeNewWindow(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
WebBrowserExtendedNavigatingEventArgs eventArgs = e as WebBrowserExtendedNavigatingEventArgs;
webBrowser.Navigate(eventArgs.Url);
eventArgs.Cancel = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private void FrmWebBrowser_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
webBrowser.Navigate("http://www.google.com/");
}
代码下载: Vs2005WebBrowser扩展NewWindow事件(下载链接)
多谢@zicerock的反馈:
请大家记得添加 Microsoft.mshtml.dll 引用。