タイトル | : Re: WebBrowserのNewWindowについて |
記事No | : 6753 |
投稿日 | : 2008/01/01(Tue) 01:04 |
投稿者 | : YAS |
VB2005のWebBrowserコントロールでU5さんがお望みの機能を実現することはちょっと面倒です。 WebBrowser.CreateSinkメソッドのヘルプを参考にして,NewWindow2イベントを実装する必要があります。 そのNewWindow2イベントの中で新しいIEが開くのを抑制することができます。 以下のコードは簡単なタブブラウザの例です。 フォームのコードにコピー&ペーストするだけで動作するはずです。参考にしてください。
※エラー処理などは適当なので,必ず内容を理解して,必要な変更をしてから利用してください。
Option Strict On
Imports System.Runtime.InteropServices Imports System.Security.Permissions
Public Class Form1
Dim TabControl1 As New TabControl Dim WebBrowser1 As ExWebBrowser Dim TabPage1 As TabPage
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'WebBrowser1 Me.WebBrowser1 = New ExWebBrowser Me.WebBrowser1.Dock = DockStyle.Fill AddHandler WebBrowser1.NewWindow2, AddressOf WebBrowser_NewWindow2 'TabPage1 Me.TabPage1 = New TabPage Me.TabPage1.Controls.Add(WebBrowser1) 'TabControl Me.TabControl1.Dock = DockStyle.Fill Me.TabControl1.TabPages.Add(TabPage1) 'Form1 Me.Text = "WebBrowserNewWindow2Event" Me.Controls.Add(Me.TabControl1) ' Me.WebBrowser1.GoHome() End Sub
Private Sub WebBrowser_NewWindow2(ByVal sender As Object, ByVal e As WebBrowserNewWindow2EventArgs) 'WebBrowser1 Me.WebBrowser1 = New ExWebBrowser Me.WebBrowser1.Dock = DockStyle.Fill AddHandler WebBrowser1.NewWindow2, AddressOf WebBrowser_NewWindow2 'TabPage1 Me.TabPage1 = New TabPage Me.TabPage1.Controls.Add(WebBrowser1) 'TabControl Me.TabControl1.Controls.Add(TabPage1) Me.TabControl1.SelectedTab = TabPage1 '新しいウィンドウが開くのを抑制 e.ppDisp = Me.WebBrowser1.Application Me.WebBrowser1.RegisterAsBrowser = True End Sub
End Class
Class ExWebBrowser Inherits WebBrowser
'NewWindow2イベントの拡張 Private cookie As AxHost.ConnectionPointCookie Private helper As WebBrowser2EventHelper
<System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _ <System.Runtime.InteropServices.DispIdAttribute(200)> _ Public ReadOnly Property Application() As Object Get If IsNothing(Me.ActiveXInstance) Then Throw New AxHost.InvalidActiveXStateException("Application", AxHost.ActiveXInvokeKind.PropertyGet) End If Return DirectCast(Me.ActiveXInstance, IWebBrowser2).Application End Get End Property
<System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _ <System.Runtime.InteropServices.DispIdAttribute(552)> _ Public Property RegisterAsBrowser() As Boolean Get If IsNothing(Me.ActiveXInstance) Then Throw New AxHost.InvalidActiveXStateException("RegisterAsBrowser", AxHost.ActiveXInvokeKind.PropertyGet) End If Return DirectCast(Me.ActiveXInstance, IWebBrowser2).RegisterAsBrowser End Get Set(ByVal value As Boolean) If IsNothing(Me.ActiveXInstance) Then Throw New AxHost.InvalidActiveXStateException("RegisterAsBrowser", AxHost.ActiveXInvokeKind.PropertySet) End If DirectCast(Me.ActiveXInstance, IWebBrowser2).RegisterAsBrowser = value End Set End Property
<PermissionSetAttribute(SecurityAction.LinkDemand, Name:="FullTrust")> _ Protected Overrides Sub CreateSink() MyBase.CreateSink() helper = New WebBrowser2EventHelper(Me) cookie = New AxHost.ConnectionPointCookie(Me.ActiveXInstance, helper, GetType(DWebBrowserEvents2)) End Sub
<PermissionSetAttribute(SecurityAction.LinkDemand, Name:="FullTrust")> _ Protected Overrides Sub DetachSink() If cookie IsNot Nothing Then cookie.Disconnect() cookie = Nothing End If MyBase.DetachSink() End Sub
Public Event NewWindow2 As WebBrowserNewWindow2EventHandler
Protected Overridable Sub OnNewWindow2(ByVal e As WebBrowserNewWindow2EventArgs) RaiseEvent NewWindow2(Me, e) End Sub
Private Class WebBrowser2EventHelper Inherits StandardOleMarshalObject Implements DWebBrowserEvents2
Private parent As ExWebBrowser
Public Sub New(ByVal parent As ExWebBrowser) Me.parent = parent End Sub
Public Sub NewWindow2(ByRef ppDisp As Object, ByRef cancel As Boolean) Implements DWebBrowserEvents2.NewWindow2 Dim e As New WebBrowserNewWindow2EventArgs(ppDisp) Me.parent.OnNewWindow2(e) ppDisp = e.ppDisp cancel = e.Cancel End Sub
End Class
End Class
Public Delegate Sub WebBrowserNewWindow2EventHandler(ByVal sender As Object, ByVal e As WebBrowserNewWindow2EventArgs)
Public Class WebBrowserNewWindow2EventArgs Inherits System.ComponentModel.CancelEventArgs
Private ppDispValue As Object
Public Sub New(ByVal ppDisp As Object) Me.ppDispValue = ppDisp End Sub
Public Property ppDisp() As Object Get Return ppDispValue End Get Set(ByVal value As Object) ppDispValue = value End Set End Property
End Class
<ComImport(), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), _ InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _ Public Interface DWebBrowserEvents2
<DispId(DISPID.NEWWINDOW2)> Sub NewWindow2( _ <[In](), Out(), MarshalAs(UnmanagedType.IDispatch)> ByRef ppDisp As Object, _ <[In](), Out()> ByRef cancel As Boolean)
End Interface
Public Enum DISPID NEWWINDOW2 = 251 End Enum
<ComImport(), Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E"), _ InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _ Public Interface IWebBrowser2
ReadOnly Property Application() As <MarshalAs(UnmanagedType.IDispatch)> Object Property RegisterAsBrowser() As <MarshalAs(UnmanagedType.VariantBool)> Boolean
End Interface
|