webbrowser 的问题。

ruhong1 2019-02-12 08:14:28

Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Imports System
Imports System.Security
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices.ComTypes
Imports System.Windows.Forms
''' <summary>扩展WebBrowser,拥有跳转前获取URL的能力</summary>
Public Class WebBrowserExt
Inherits WebBrowser

Shadows cookie As AxHost.ConnectionPointCookie
Shadows events As WebBrowserExtEvents

Protected Overrides Sub CreateSink()
MyBase.CreateSink()
events = New WebBrowserExtEvents(Me)
cookie = New AxHost.ConnectionPointCookie(Me.ActiveXInstance, events, GetType(DWebBrowserEvents2))
End Sub

Protected Overrides Sub DetachSink()
If Not cookie Is Nothing Then
cookie.Disconnect()
cookie = Nothing
End If
MyBase.DetachSink()
End Sub

''' <summary>在跳转前</summary>
Public Event BeforeNavigate(ByVal sender As Object, ByVal e As NavEventArgsExt)
''' <summary>在弹出新窗体前</summary>
Public Event BeforeNewWindow(ByVal sender As Object, ByVal e As NavEventArgsExt)

Protected Sub OnBeforeNewWindow(ByVal url As String, ByRef cancel As Boolean)
Dim args As New NavEventArgsExt(url, Nothing)
RaiseEvent BeforeNewWindow(Me, args)
cancel = args.Cancel
End Sub

Protected Sub OnBeforeNavigate(ByVal url As String, ByVal frame As String, ByRef cancel As Boolean)
Dim args As New NavEventArgsExt(url, frame)
RaiseEvent BeforeNavigate(Me, args)
cancel = args.Cancel
End Sub



''' <summary>跳转事件封包</summary>
Public Class NavEventArgsExt
Inherits CancelEventArgs

Sub New(ByVal url As String, ByVal frame As String)
MyBase.New()
_Url = url
_Frame = frame
End Sub

Private _Url As String
ReadOnly Property Url As String
Get
Return _Url
End Get
End Property

Private _Frame As String
ReadOnly Property Frame As String
Get
Return _Frame
End Get
End Property
End Class


Private Class WebBrowserExtEvents
Inherits StandardOleMarshalObject
Implements DWebBrowserEvents2

Dim _browser As WebBrowserExt
Sub New(ByVal browser As WebBrowser)
_browser = browser
End Sub

Public Sub BeforeNavigate2(ByVal pDisp As Object, ByRef url As Object, ByRef flags As Object, ByRef targetFrameName As Object, ByRef postData As Object, ByRef headers As Object, ByRef cancel As Boolean) Implements DWebBrowserEvents2.BeforeNavigate2
_browser.OnBeforeNavigate(CType(url, String), CType(targetFrameName, String), cancel)
End Sub

Public Sub NewWindow3(ByVal pDisp As Object, ByRef cancel As Boolean, ByRef flags As Object, ByRef URLContext As Object, ByRef URL As Object) Implements DWebBrowserEvents2.NewWindow3
_browser.OnBeforeNewWindow(CType(URL, String), cancel)
End Sub
End Class

<ComImport(), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch), _
TypeLibType(TypeLibTypeFlags.FHidden)> _
Public Interface DWebBrowserEvents2

<DispId(250)> _
Sub BeforeNavigate2(<[In](), MarshalAs(UnmanagedType.IDispatch)> ByVal pDisp As Object, <[In]()> ByRef url As Object, <[In]()> ByRef flags As Object, <[In]()> ByRef targetFrameName As Object, <[In]()> ByRef postData As Object, <[In]()> ByRef headers As Object, <[In](), Out()> ByRef cancel As Boolean)

<DispId(273)> _
Sub NewWindow3(<[In](), MarshalAs(UnmanagedType.IDispatch)> ByVal pDisp As Object, <[In](), Out()> ByRef cancel As Boolean, <[In]()> ByRef flags As Object, <[In]()> ByRef URLContext As Object, <[In]()> ByRef URL As Object)

End Interface

Enum WindowsMessages
WM_WINDOWPOSCHANGED = &H47
WM_WINDOWPOSCHANGING = &H46
WM_WININICHANGE = &H1A
End Enum
<PermissionSet(SecurityAction.LinkDemand, Name:="FullTrust")>
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = CInt(WindowsMessages.WM_PARENTNOTIFY) Then
Dim wp As Integer = m.WParam.ToInt32()
Dim X As Integer = wp And &HFFFF
If X = CInt(WindowsMessages.WM_DESTROY) Then Me.OnQuit()
End If

MyBase.WndProc(m)
End Sub
Public Event Quit As EventHandler
Protected Sub OnQuit()
RaiseEvent Quit(Me, EventArgs.Empty)
End Sub

#Region "禁用网页跳转声"

Dim _disableNavigationSounds As Boolean = False

''' <summary>禁止链接跳转声</summary>
Property DisableNavigationSounds As Boolean
Get
Return _disableNavigationSounds
End Get
Set(ByVal value As Boolean)
If _disableNavigationSounds = value Then Return
CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_PROCESS, value)
_disableNavigationSounds = value
End Set
End Property


Const FEATURE_DISABLE_NAVIGATION_SOUNDS As Integer = 21
Const SET_FEATURE_ON_PROCESS As Integer = &H2

<DllImport("urlmon.dll"), PreserveSig()> _
Private Shared Function CoInternetSetFeatureEnabled(ByVal FeatureEntry As Integer, <MarshalAs(UnmanagedType.U4)> ByVal dwFlags As Integer, ByVal fEnable As Boolean) As <MarshalAs(UnmanagedType.[Error])> Integer
End Function


#End Region

End Class


这是从网上整理的webbrowser控件的扩展组件,其中 Enum WindowsMessages 由于太长,我去掉了,只留了2条。
现在的问题是,作一个C/S的软件,在窗体中加入这个组件,
在BeforeNewWindow事件中,获取的URL为NULL。
第二个问题是,当IE要关闭这个窗口时,他会提示是否关闭窗口,不想要这个提示。
各位老大能帮忙改就改一下,要不告诉我哪有最完善的webbrowser的扩展。
第三方浏览控件用不了,单位的OA限死了,只认IE。


...全文
82 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruhong1 2019-02-14
  • 打赏
  • 举报
回复
上述的webbrowser能正常浏览网页,但是就是加载不了ntko office控件,如何做?

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧