Hallo Binthom, Servus Steve
es gibt eine andere Lösung. Dazu muss man das Webrowsercontrol erweitern und die DWebBrowserEvents2 selbst abfangen.
Interface DWebBrowserEvents2 nachbauen
Imports System.Runtime.InteropServices
<ComImport(), _
InterfaceType(ComInterfaceType.InterfaceIsIDispatch), _
TypeLibType(TypeLibTypeFlags.FHidden), _
Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D")> _
Public Interface DWebBrowserEvents2
'' Methods
<DispId(270)> _
Sub FileDownload( _
<Out(), _
MarshalAs(UnmanagedType.VariantBool)> ByRef activeDoc As Boolean, _
<[In](), Out(), MarshalAs(UnmanagedType.VariantBool)> ByRef cancel As Boolean)
End Interface Dann die DwebBrowserEvents2 in eine Klasse implementieren
Imports System.Runtime.InteropServices
Friend Class DWebBrowserEvents2Impl
Inherits StandardOleMarshalObject
Implements DWebBrowserEvents2
Public Sub FileDownloadImpl( _
ByRef activeDoc As Boolean, _
ByRef cancel As Boolean) _
Implements DWebBrowserEvents2.FileDownload
RaiseEvent FileDownload(activeDoc, cancel)
End Sub
Public Event FileDownload(ByRef activeDoc As Boolean, ByRef cancel As Boolean)
End Class Anschließend von WebBrowser ableiten und ein paar Methoden überschreiben und einen neuen Event hinzutun
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Public Class Browser
Inherits WebBrowser
Private _cookieEvents As AxHost.ConnectionPointCookie
Private WithEvents _browserEvents As DWebBrowserEvents2Impl
Protected Overrides Sub CreateSink()
MyBase.CreateSink()
_browserEvents = New DWebBrowserEvents2Impl
_cookieEvents = New AxHost.ConnectionPointCookie( _
Me.ActiveXInstance, _browserEvents, GetType( _
DWebBrowserEvents2))
End Sub
Protected Overrides Sub DetachSink()
If Not _cookieEvents Is Nothing Then
_cookieEvents.Disconnect()
_cookieEvents = Nothing
End If
MyBase.DetachSink()
End Sub
Private Sub _browserEvents_FileDownload(ByRef activeDoc As Boolean, ByRef _
cancel As Boolean) _
Handles _browserEvents.FileDownload
Dim e As New FileDownloadEventArgs(activeDoc, cancel)
OnFileDownloadEx(e)
cancel = e.Cancel
End Sub
<DebuggerStepThrough()> _
Public Class FileDownloadEventArgs
Inherits CancelEventArgs
Public Sub New(ByVal activeDoc As Boolean, ByVal cncl As Boolean)
MyBase.New(cncl)
_isActiveDoc = activeDoc
End Sub
Private _isActiveDoc As Boolean
Public ReadOnly Property IsActiveDocument() As Boolean
Get
Return _isActiveDoc
End Get
End Property
End Class
Public Delegate Sub FileDownloadEventHandler(ByVal sender As Object, ByVal e _
As FileDownloadEventArgs)
Public Event FileDownloadEx As FileDownloadEventHandler
Protected Overridable Sub OnFileDownloadEx(ByVal e As FileDownloadEventArgs)
RaiseEvent FileDownloadEx(Me, e)
End Sub
End Class Nun den abgeleiteten Browser in eine Form tun und im Navigating-Event sich merken, wohin er Navigieren will, damit man im FileDownloadEx-Event auch weiß welche Datei runtergeladen werden soll.
In einer Form mit einem Browser namens Browser1:
Private _beforeNavUrl As Uri
Private Sub Browser1_Navigating(ByVal sender As Object, _
ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) _
Handles Browser1.Navigating
_beforeNavUrl = e.Url
End Sub
Private Sub Browser1_FileDownloadEx(ByVal sender As Object, ByVal e As _
Browser.Browser.FileDownloadEventArgs) _
Handles Browser1.FileDownloadEx
If Not e.IsActiveDocument Then
e.Cancel = True
MsgBox("Download von " & _beforeNavUrl.ToString)
End If
End Sub Ciao
D. |