System.Windows.Forms.WebBrowser には NewWindow2 イベントが無い。
なぜこのイベントを使うかと言うと、たとえばタブブラウザを作りたい場合、
WebBrowser コンポーネントをそのまま使うと別ウィンドウでリンクを開く場合にIEが起動してしまう。
NewWindow イベントで e.Cancel = true にすると IEは起動しないが、Navigate もされない(イベント自体がキャンセル)。
NewWindow2 で新しいタブとブラウザを用意してそこに表示させるにはこのイベントを利用しなければならない。
WebBrowser を拡張した ExWebBrowser というものが さんのサイトにある。
しかしこれはVBだったので、このソースをC#に書き換えてみた。
ネームスペースが.NET 標準ライブラリのネームスペースなので、直したほうがいいかも。
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Windows.Forms;
using SHDocVw;
namespace System.Windows.Forms
{
public class ExWebBrowser : System.Windows.Forms.WebBrowser
{
//NewWindow2イベントの拡張
private AxHost.ConnectionPointCookie cookie;
private WebBrowser2EventHelper helper;
[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
[System.Runtime.InteropServices.DispIdAttribute(200)]
public object Application
{
get
{
if (this.ActiveXInstance == null)
{
throw new AxHost.InvalidActiveXStateException("Application", AxHost.ActiveXInvokeKind.PropertyGet);
}
return ((SHDocVw.IWebBrowser2)this.ActiveXInstance).Application;
}
}
[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
[System.Runtime.InteropServices.DispIdAttribute(552)]
public bool RegisterAsBrowser
{
get
{
if (this.ActiveXInstance == null)
{
throw new AxHost.InvalidActiveXStateException("RegisterAsBrowser", AxHost.ActiveXInvokeKind.PropertyGet);
}
return ((SHDocVw.IWebBrowser2)this.ActiveXInstance).RegisterAsBrowser;
}
set
{
if (this.ActiveXInstance == null)
{
throw new AxHost.InvalidActiveXStateException("RegisterAsBrowser", AxHost.ActiveXInvokeKind.PropertySet);
}
((SHDocVw.IWebBrowser2)this.ActiveXInstance).RegisterAsBrowser = value;
}
}
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
protected override void CreateSink()
{
base.CreateSink();
helper = new WebBrowser2EventHelper(this);
cookie = new AxHost.ConnectionPointCookie(this.ActiveXInstance, helper, typeof(DWebBrowserEvents2));
}
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
protected override void DetachSink()
{
if (cookie != null)
{
cookie.Disconnect();
cookie = null;
}
base.DetachSink();
}
public event WebBrowserNewWindow2EventHandler NewWindow2;
protected virtual void OnNewWindow2(WebBrowserNewWindow2EventArgs e)
{
NewWindow2(this, e);
}
private class WebBrowser2EventHelper : StandardOleMarshalObject, DWebBrowserEvents2
{
private ExWebBrowser parent;
public WebBrowser2EventHelper(ExWebBrowser parent)
{
this.parent = parent;
}
public void NewWindow2(ref object ppDisp, ref bool cancel)
{
WebBrowserNewWindow2EventArgs e = new WebBrowserNewWindow2EventArgs(ppDisp);
this.parent.OnNewWindow2(e);
ppDisp = e.ppDisp;
cancel = e.Cancel;
}
}
}
public delegate void WebBrowserNewWindow2EventHandler(object sender, WebBrowserNewWindow2EventArgs e);
public class WebBrowserNewWindow2EventArgs : System.ComponentModel.CancelEventArgs
{
private object ppDispValue;
public WebBrowserNewWindow2EventArgs(object ppDisp)
{
this.ppDispValue = ppDisp;
}
public object ppDisp
{
get
{
return ppDispValue;
}
set
{
ppDispValue = value;
}
}
}
[ComImport(), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch), TypeLibType(TypeLibTypeFlags.FHidden)]
public interface DWebBrowserEvents2
{
[DispId((int)DISPID.NEWWINDOW2)]
void NewWindow2(
[InAttribute(), OutAttribute(), MarshalAs(UnmanagedType.IDispatch)] ref object ppDisp,
[InAttribute(), OutAttribute()] ref bool cancel
);
}
public enum DISPID
{
NEWWINDOW2 = 251
}
}