我想出了这个。正如它所说的,您需要为HttpUtility.UrlEncode添加对“System.Web”的引用。此外,代码中还有一些“选项”注释掉了。取自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
太棒了!我正在努力解决如何做到这一点,并不能得到它。 – 2009-11-06 16:25:12
这不适用于我的VS 2005安装 - 它什么都不做 - 没有错误信息。如果我将DTE.ExecuteCommand更改为System.Diagnostics.Process.Start(strUrl),那么它将在我的默认浏览器中打开该URL,但浏览器未获得焦点。什么是“导航”应该做的。这是记录在任何地方?和“/ ext”,这一切似乎都没有在msdn页面提及ExecuteCommand。任何想法如何解决这个问题?谢谢。 – 2010-08-17 11:50:01
对不起,安德鲁。我很久以前就开始转向Visual Studio的版本,该版本不再支持宏/脚本。 – 2015-06-04 14:15:08