1

我花了过去 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
4

3 回答 3

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

未经测试,但你明白了。

于 2012-11-27T20:20:09.093 回答
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
于 2012-11-27T21:25:59.560 回答
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

希望这可以帮助。

于 2014-02-17T16:05:18.430 回答