2009-08-26 63 views
5

我發現內置的Visual Studio文檔資源管理器不到相關的,尤其是當我工作的更多SDK有最新的在線內容時。按下F1通常會啓動文檔資源管理器,但無法使用任何其他內容。從Visual Studio中加載瀏覽器中的搜索URL

有沒有在Visual Studio中的按組合鍵的任何方式:

  • 默認瀏覽器打開一個搜索引擎查詢使用
  • 的URL在當前的關鍵字光標位置
  • 過濾器被添加諸如site:msdn.microsoft.com

我不知道在VS但是p巨集什麼這是我需要的。有誰知道如何去設置這個? teh codez會很好!

回答

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 
+0

太棒了!我正在努力解決如何做到這一點,並不能得到它。 – 2009-11-06 16:25:12

+0

這不適用於我的VS 2005安裝 - 它什麼都不做 - 沒有錯誤信息。如果我將DTE.ExecuteCommand更改爲System.Diagnostics.Process.Start(strUrl),那麼它將在我的默認瀏覽器中打開該URL,但瀏覽器未獲得焦點。什麼是「導航」應該做的。這是記錄在任何地方?和「/ ext」,這一切似乎都沒有在msdn頁面提及E​​xecuteCommand。任何想法如何解決這個問題?謝謝。 – 2010-08-17 11:50:01

+0

對不起,安德魯。我很久以前就開始轉向Visual Studio的版本,該版本不再支持宏/腳本。 – 2015-06-04 14:15:08

2

使用我想出了這裏面的鏈接Preet provided啓動默認的瀏覽器:

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

我想出了這個。正如它所說的,您需要爲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 
相關問題