.net里的webbrowser,如何准确获取弹出窗口的网址,并在自己的新窗口中继承打开。
Private Sub Web1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Web1.NewWindow
Dim NewURL As String = CType(sender, Windows.Forms.WebBrowser).StatusText
Dim myfrm As New FormWeb
e.Cancel = True
myfrm.Show()
myfrm.Web1.Navigate(NewURL)
End Sub
这段代码是我之前用的,对于普通文字链接是可以正确弹出网页,但对于网页内用按钮弹出,或是javascript弹出的,就无法获取正确的弹出网址。或者说是没有继承性,若是防盗链的网站用navigate直接打开新网址估计也会出错。
请问还有什么更规范的写法或办法可以解决这问题,实现类似于一些腾讯TT第三方的浏览器。
注意,我用的是.net里自带的那个Webbrowser,而不是以前6.0里那个axWebbrowser,它俩提供的方法完全不同。后者好象是可以用ppdisp实现,但我希望得到前者的解答。望指教!
[解决办法]
好久没做过Web了,忘了,帮顶一下,学习学习
[解决办法]
利用网页元素
ELE开头的
[解决办法]
WebBrowser1.Document.Url
[解决办法]
对于这个问题我也一直很困惑,我也很想知道解决方案。帮你顶了
[解决办法]
我的做法并不正统.我是通过监视状态栏来取得的:
- VB.NET code
Namespace LzmTW.uSystem.uWindows.uForms.Web <Docking(DockingBehavior.AutoDock), Designer("System.Windows.Forms.Design.WebBrowserDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), ClassInterface(ClassInterfaceType.AutoDispatch), DefaultEvent("DocumentCompleted"), ComVisible(True), DefaultProperty("Url")> _ <System.Drawing.ToolboxBitmap(GetType(System.Windows.Forms.WebBrowser))> _ Public Class WebBrowserEx Inherits System.Windows.Forms.WebBrowser Friend Const DefaultUri As String = "about:blank" Private WithEvents gComInternetExplorer As SHDocVw.InternetExplorer Private gCanNewWindow As Boolean = True Public Property CanNewWindow() As Boolean Get Return Me.gCanNewWindow End Get Set(ByVal value As Boolean) Me.gCanNewWindow = value End Set End Property Sub New() Me.Url = New System.Uri(DefaultUri, System.UriKind.Absolute) End Sub Private Sub WebBrowser_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Me.HandleCreated Me.gComInternetExplorer = CType(Me.ActiveXInstance, SHDocVw.InternetExplorer) With gStatusList .Add(SHDocVw.OLECMDID.OLECMDID_CUT, False) .Add(SHDocVw.OLECMDID.OLECMDID_COPY, False) .Add(SHDocVw.OLECMDID.OLECMDID_PASTE, False) End With End Sub ''按钮、状态栏的隐藏显示 ''动作由菜单发出 Public Event StatusBarVisibleChanged As EventHandler Public Event AddressBarVisibleChanged As EventHandler Public Event LinkBarVisibleChanged As EventHandler Public Event StandarBarVisibleChanged As EventHandler Private gStatusBarVisible As Boolean Private gAddressBarVisible As Boolean Private gLinkBarVisible As Boolean Private gStandarBarVisible As Boolean Public Property StatusBarVisible() As Boolean Get Return Me.gStatusBarVisible End Get Set(ByVal value As Boolean) If Me.gStatusBarVisible <> value Then Me.gStatusBarVisible = value RaiseEvent StatusBarVisibleChanged(Me, New EventArgs) End If End Set End Property Public Property AddressBarVisible() As Boolean Get Return Me.gAddressBarVisible End Get Set(ByVal value As Boolean) If Me.gAddressBarVisible <> value Then Me.gAddressBarVisible = value RaiseEvent AddressBarVisibleChanged(Me, New EventArgs) End If End Set End Property Public Property LinkBarVisible() As Boolean Get Return Me.gLinkBarVisible End Get Set(ByVal value As Boolean) If Me.gLinkBarVisible <> value Then Me.gLinkBarVisible = value RaiseEvent LinkBarVisibleChanged(Me, New EventArgs) End If End Set End Property Public Property StandarBarVisible() As Boolean Get Return Me.gStandarBarVisible End Get Set(ByVal value As Boolean) If Me.gStandarBarVisible <> value Then Me.gStandarBarVisible = value RaiseEvent StandarBarVisibleChanged(Me, New EventArgs) End If End Set End Property ''New Window Private gNewWindowUri As Uri = New Uri(DefaultUri) Public ReadOnly Property NewWindowUri() As Uri Get Return gNewWindowUri End Get End Property Protected Overrides Sub OnStatusTextChanged(ByVal e As System.EventArgs) If UrlHelper.Common.IsValidURL(Me.StatusText) Then gNewWindowUri = New Uri(Me.StatusText) End If MyBase.OnStatusTextChanged(e) End Sub Protected Overrides Sub OnNewWindow(ByVal e As System.ComponentModel.CancelEventArgs) Dim mElement As HtmlElement = Me.Document.ActiveElement Dim mHref As String = mElement.GetAttribute("href") If Not String.IsNullOrEmpty(mHref) Then gNewWindowUri = New Uri(mHref) End If If Not Me.CanNewWindow Then e.Cancel = True Me.Navigate(Me.NewWindowUri) Else MyBase.OnNewWindow(e) End If End Sub End ClassEnd Namespace
[解决办法]
用这个扩展后的控件吧
- VB.NET code
Imports System.RuntimeImports System.ComponentModelPublic Class ExtendedWebBrowser Inherits WebBrowser Private cookie As AxHost.ConnectionPointCookie Private Shadows events As WebBrowserExtendedEvents Private Const WM_PARENTNOTIFY As Integer = &H210 Private Const WM_DESTROY As Integer = 2 Public Event WindowClosing() Public Event NewWindowWithTaget As EventHandler(Of WebBrowserExtendedNavigatingEventArgs) Protected Overrides Sub WndProc(ByRef m As Message) Select Case m.Msg Case WM_PARENTNOTIFY If (Not DesignMode) AndAlso (m.WParam = WM_DESTROY) Then RaiseEvent WindowClosing() DefWndProc(m) Case Else MyBase.WndProc(m) End Select End Sub Protected Overloads Overrides Sub CreateSink() MyBase.CreateSink() events = New WebBrowserExtendedEvents(Me) cookie = New AxHost.ConnectionPointCookie(Me.ActiveXInstance, events, GetType(DWebBrowserEvents2)) End Sub Protected Overloads Overrides Sub DetachSink() If cookie IsNot Nothing Then cookie.Disconnect() cookie = Nothing End If MyBase.DetachSink() End Sub Protected Sub OnNewWindow3(ByVal url As String, ByVal e As WebBrowserExtendedNavigatingEventArgs) RaiseEvent NewWindowWithTaget(Me, e) End Sub Private Class WebBrowserExtendedEvents Inherits System.Runtime.InteropServices.StandardOleMarshalObject Implements DWebBrowserEvents2 Private _Browser As ExtendedWebBrowser Public Sub New(ByVal browser As ExtendedWebBrowser) _Browser = browser End Sub Public Sub NewWindow3(ByVal pDisp As Object, ByRef cancel As Boolean, ByRef flags As Object, ByRef hostURL As Object, ByRef URL As Object) _ Implements DWebBrowserEvents2.NewWindow3 Dim args As New WebBrowserExtendedNavigatingEventArgs(URL) args.Cancel = cancel _Browser.OnNewWindow3(URL, args) cancel = args.Cancel End Sub End Class <InteropServices.ComImport(), InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), _ InteropServices.InterfaceTypeAttribute(InteropServices.ComInterfaceType.InterfaceIsIDispatch), _ InteropServices.TypeLibType(InteropServices.TypeLibTypeFlags.FHidden)> _ Public Interface DWebBrowserEvents2 <InteropServices.DispId(273)> _ Sub NewWindow3(ByVal pDisp As Object, ByRef cancel As Boolean, ByRef flags As Object, ByRef hostURL As Object, ByRef URL As Object) End InterfaceEnd ClassPublic Class WebBrowserExtendedNavigatingEventArgs Inherits CancelEventArgs Private _Url As String Public Sub New(ByVal url As String) _Url = url End Sub Public ReadOnly Property Url() As String Get Return _Url End Get End PropertyEnd Class'调用时使用下面的事件Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow Dim NewUrl As String NewUrl = CType(sender, Windows.Forms.WebBrowser).Document.ActiveElement.GetAttribute("href") ''NewUrl = CType(sender, Windows.Forms.WebBrowser).StatusText RaiseEvent NewExplorer(NewUrl)'打开新的一页或窗体 e.Cancel = True End Sub Private Sub WebBrowser1_NewWindowWithTaget(ByVal sender As Object, ByVal e As WebBrowserExtendedNavigatingEventArgs) Handles WebBrowser1.NewWindowWithTaget RaiseEvent NewExplorer(e.Url)'打开新的一页或窗体 e.Cancel = True End Sub
[解决办法]
学习~
[解决办法]
O_O
------解决方案--------------------
lzmtw 写的看了头都大了
因为不想头大,所以就没有仔细看了