在默认浏览器中打开所有超链接

时间:2012-11-27 20:13:01

标签: vb.net windows-forms-designer

我花了3个小时试图找到一个解决方案,但没有多少。有些人很亲密,但对我来说很有用。 (Process.Start或shell的东西对我不起作用,因为我希望特定网站中的所有超链接都在默认浏览器而不是IE中打开。下面是我正在使用的代码。

提前致谢。

 Try
        If IsConnectionAvailable("http://google.com") = True Then
            MapBrowser.Navigate("http://localhost")
            '  Me.BackgroundImage = System.Drawing.Image.FromFile(ImgBackground)
        Else
            MapBrowser.Navigate("http://yahoo.com")
          Timer4.Start()
        End If

    Catch ex As Exception
        MsgBox(ex.Message())
    End Try

3 个答案:

答案 0 :(得分:1)

在浏览器控件上捕获Navigating事件,然后在新窗口中处理要打开的事件。 Process.Start()将打开默认浏览器。如果它为您打开IE,那么IE就是您的默认浏览器。

Private Sub webBrowser1_Navigating( _
    ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs) _
    Handles webBrowser1.Navigating

    If Not (e.Url = "some URL you don't want to pop up in new window") Then
        Process.Start(e.Url.ToString())
        e.Cancel = true
    End If
End Sub

未经测试,但你明白了。

答案 1 :(得分:0)

Private Sub webBrowser1_Navigating(ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs) Handles webBrowser1.Navigating
'if the url isnt a part of your site
If Not e.Url.ToString().Contains("http://yoursite.com") Then
    Process.Start(e.Url.ToString())
    e.Cancel = true
End If
End Sub

答案 2 :(得分:0)

基于Brad的示例,您可以制作类似这样的内容,它将加载Web浏览器控件中的第一页以及默认浏览器中的所有后续链接:

Private Sub Form1_Leave(sender As Object, e As EventArgs) Handles Me.FormClosing
    OpenAllInBrowser = False   ' This is to reset the variable to False, because
End Sub                        ' sometimes closing the form doesn't reset the variables.

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    OpenAllInBrowser = True
End Sub

Private OpenAllInBrowser As Boolean = False

Private Sub webBrowser1_Navigating(ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
    If OpenAllInBrowser Then
        Process.Start(e.Url.ToString())
        e.Cancel = True
    End If
End Sub

希望这有帮助。