您的位置:首页 > 编程语言 > VB

在VB.NET中如何使在Webbrowser中实现标签页中打开新链接

2006-10-02 10:50 711 查看
操作步骤:
1、在工程中添加COM的Microsoft Internet Controls的引用(这个引用对应的文件是shdocvw.dll,通常在system32目录下)。
2、添加如下代码:

Public Class Form1
Private Sub NewWindow3(ByRef ppDisp As Object, ByRef Cancel As Boolean, _
ByVal dwFlags As UInteger, _
ByVal bstrUrlContext As String, ByVal bstrUrl As String)

Dim xPage As TabPage = New TabPage
xPage.Text = "abcd"
TabControl1.TabPages.Add(xPage)

Dim x As New WebBrowser
DirectCast(WebBrowser1.ActiveXInstance, SHDocVw.WebBrowser).RegisterAsBrowser = True

xPage.Controls.Add(x)
x.Dock = DockStyle.Fill
x.Visible = True

x.Navigate(bstrUrl)
Cancel = True
End Sub

Private Sub NewWindow2(ByRef ppDisp As Object, ByRef Cancel As Boolean)
Dim xPage As TabPage = New TabPage
xPage.Text = "abcd"
TabControl1.TabPages.Add(xPage)

Dim x As New WebBrowser
DirectCast(WebBrowser1.ActiveXInstance, SHDocVw.WebBrowser).RegisterAsBrowser = True

xPage.Controls.Add(x)
x.Dock = DockStyle.Fill
x.Visible = True
x.Navigate("about:blank")

ppDisp = x.ActiveXInstance 'DirectCast(x.ActiveXInstance, SHDocVw.WebBrowser).Application
'Cancel = True
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在窗体载入后设置Webbrowser的NewWindow3事件处理函数
'AddHandler DirectCast(WebBrowser1.ActiveXInstance, SHDocVw.WebBrowser).NewWindow3, AddressOf NewWindow3
'如果不是Windows XP SP2的话需要监控NewWindow2消息
AddHandler DirectCast(WebBrowser1.ActiveXInstance, SHDocVw.WebBrowser).NewWindow2, AddressOf NewWindow2

WebBrowser1.Navigate("http://www.applevb.com")
End Sub

End Class

运行程序,可以看到新打开的窗口都会在TabControl1的新标签页中打开.在上面的代码中,如果是Windows XP+SP2的话可以监控NewWindow3事件,否侧需要监控NewWindow2事件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: