从Visual Studio中在浏览器中加载搜索URL

5

我发现内置的Visual Studio文档资源管理器越来越不够用了,特别是随着我使用的SDK中有更多最新内容在线上。按下F1键通常会启动文档资源管理器,但它并没有帮助到我,已经无法使用。

在Visual Studio中是否有一种方式:

  • 按下某个组合键会打开默认浏览器,并跳转到搜索引擎URL
  • 查询关键字为当前光标位置下的关键字
  • 添加筛选条件,例如site:msdn.microsoft.com

我不知道VS中的宏,但我想这就是我需要的。有人知道如何设置吗?提供代码将是很好的帮助!

3个回答

5
这里是另一种版本(基于Alex的答案),它还将选择您正在使用的当前单词。更像典型的F1帮助。
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Search
    Sub GoogleSearch()
        AnySearch("http://www.google.com/search?q=.net+")
    End Sub

    Sub BingSearch()
        AnySearch("http://www.bing.com/search?q=")
    End Sub

    Private Sub AnySearch(ByVal searchUrl)
        Dim strUrl As String
        Dim selection As String = GetSelection()
        If selection <> "" Then
            strUrl = searchUrl + selection
            DTE.ExecuteCommand("nav", strUrl & " /ext")
        Else
            MsgBox("Select text to search for.")
        End If
    End Sub

    Private Function GetSelection() As String
        Dim selection As TextSelection = DTE.ActiveDocument.Selection()
        If selection.Text <> "" Then
            Return selection.Text
        Else
            DTE.ExecuteCommand("Edit.SelectCurrentWord")
            selection = DTE.ActiveDocument.Selection()
            Return selection.Text
        End If
    End Function
End Module

太棒了!我一直在尝试解决这个问题,但是一直无法解决。 - Alex Angas
这在我的VS 2005安装上不起作用 - 它什么也不做 - 没有错误消息。如果我将DTE.ExecuteCommand更改为System.Diagnostics.Process.Start(strUrl),那么它会在我的默认浏览器中打开URL,但是浏览器没有获得焦点。 "nav"应该做什么。这是否有记录?而且“/ ext”,所有这些都似乎像魔术一样,在ExecuteCommand的msdn页面中没有提到。有什么想法如何解决这个问题吗?谢谢。 - Andrew Bainbridge
抱歉回复不及时,安德鲁。我早已转向一种不再支持宏/脚本的 Visual Studio 版本。 - Michael Silver

2

使用Preet提供的链接,我编写了下面这段代码,可以启动默认浏览器:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Search
    Sub GoogleSearch()
        AnySearch("http://www.google.com/search?q=")
    End Sub

    Sub BingSearch()
        AnySearch("http://www.bing.com/search?q=")
    End Sub

    Private Sub AnySearch(ByVal searchUrl)
        Dim strUrl As String
        Dim selection As TextSelection = DTE.ActiveDocument.Selection()
        If selection.Text <> "" Then
            strUrl = searchUrl + selection.Text
            DTE.ExecuteCommand("nav", strUrl & " /ext")
        Else
            MsgBox("Select text to search for.")
        End If
    End Sub
End Module

1
我想到了这个方法。正如它所说,您需要添加对“System.Web”的引用以使用HttpUtility.UrlEncode。此外,代码中还有一些被注释掉的“选项”。其中一些部分取自http://www.codinghorror.com/blog/2005/10/google-search-vsnet-macro.html,但经过了很大的改进(在我看来)。
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module HelpFindInBrowser
    Sub HelpFindInBrowser()
        Dim s As String = ActiveWindowSelection().Trim()
        If s.Length > 0 Then
            OpenURL("http://www.google.com/search?q=" & _
                Web.HttpUtility.UrlEncode(s))
            '--------------------------------------------------------------------------

            'You will need to add a reference to 'System.Web' for HttpUtility.UrlEncode !!!

            '--------------------------------------------------------------------------
        End If
    End Sub

    Private Sub OpenURL(ByVal inURL As String)
        'specify a non default browser
        'DTE.ExecuteCommand("Tools.Shell", "notepad.exe " & inURL)

        'use the default browser:
        DTE.ExecuteCommand("nav", inURL & " /ext")

        'to have it in a new visual studio window:
        'DTE.ItemOperations.Navigate(inURL, EnvDTE.vsNavigateOptions.vsNavigateOptionsNewWindow)

        'to have it in the default visual studio browser window:
        'DTE.ItemOperations.Navigate(inURL, EnvDTE.vsNavigateOptions.vsNavigateOptionsDefault)
    End Sub

    'large part taken from http://www.codinghorror.com/blog/2005/10/google-search-vsnet-macro.html
    Private Function ActiveWindowSelection() As String
        If DTE.ActiveWindow.ObjectKind = EnvDTE.Constants.vsWindowKindOutput Then
            Return OutputWindowSelection()
        End If
        If DTE.ActiveWindow.ObjectKind = "{57312C73-6202-49E9-B1E1-40EA1A6DC1F6}" Then
            Return HTMLEditorSelection()
        End If
        Return SelectionText(DTE.ActiveWindow.Selection)
    End Function

    Private Function HTMLEditorSelection() As String
        Dim hw As HTMLWindow = ActiveDocument.ActiveWindow.Object
        Dim tw As TextWindow = hw.CurrentTabObject
        Return SelectionText(tw.Selection)
    End Function

    Private Function OutputWindowSelection() As String
        Dim w As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
        Dim ow As OutputWindow = w.Object
        Dim owp As OutputWindowPane = ow.OutputWindowPanes.Item(ow.ActivePane.Name)
        Return SelectionText(owp.TextDocument.Selection)
    End Function

    Private Function SelectionText(ByVal sel As EnvDTE.TextSelection) As String
        If sel Is Nothing Then
            Return ""
        End If

        Dim s As String

        If sel.Text.Length = 0 Then
            s = GetWordUnderCursor(sel)
        Else
            s = sel.Text
        End If
        'We could limit the text to some minimal size
        'If sel.Text.Length <= 2 Then
        'Return ""
        'End If
        Return s
    End Function

    Private Function GetWordUnderCursor(ByVal sel As EnvDTE.TextSelection) As String
        Dim ptStart As EnvDTE.EditPoint = sel.ActivePoint.CreateEditPoint()

        Dim theSelectToRight As Boolean = False

        If (ptStart.AtStartOfLine) Then
            theSelectToRight = True
        Else
            'See if there's a space to the left of ptStart
            'not at start of line, so moving one left is safe
            ptStart.CharLeft()

            If (ptStart.GetText(1).Trim() = "") Then
                theSelectToRight = True
            End If

            'Back to original position
            ptStart.CharRight()
        End If


        If (Not theSelectToRight) Then
            ptStart.WordLeft(1)
        End If

        Dim ptEnd As EnvDTE.EditPoint = ptStart.CreateEditPoint()
        ptEnd.WordRight(1)

        Return ptStart.GetText(ptEnd)
    End Function

End Module

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接