2009-08-26 13 views
5

Sto trovando il Visual Studio Document Explorer incorporato meno rilevante, soprattutto perché più degli SDK con cui lavoro hanno il contenuto più aggiornato on-line. Premendo F1 si avvia Document Explorer di solito con qualcosa di inutile e non è più utilizzabile per me.Carica URL di ricerca nel browser da Visual Studio

C'è un modo che sulla pressione di una combinazione di tasti in Visual Studio:

  • il browser predefinito apre l'URL di un motore di ricerca
  • query utilizzata è la parola chiave sotto la corrente posizione del cursore
  • un filtro viene aggiunto come site:msdn.microsoft.com

io non so nulla di macro in VS, ma p questo è ciò che ho bisogno. Qualcuno sa come fare per impostare questo? teh codez sarebbe bello!

risposta

5

Ecco un'altra versione (in base alla risposta di Alex) che sarà anche selezionare la parola corrente ci si trova. Più simile al tipico aiuto 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

Impressionante! Stavo cercando di capire come farlo e non potevo capirlo. –

+0

Questo non funziona sulla mia installazione di VS 2005 - non fa proprio niente - nessun messaggio di errore. Se cambio DTE.ExecuteCommand in System.Diagnostics.Process.Start (strUrl), quindi apre l'URL nel mio browser predefinito, ma il browser non ha il focus. Cosa dovrebbe fare "nav". È documentato da qualche parte? E il "/ ext", tutto questo sembra magia non menzionato nella pagina msdn di ExecuteCommand. Qualche idea su come posso risolvere questo problema? Grazie. –

+0

Ci scusiamo per la mancanza di risposta, Andrew. Ho passato da tempo una versione di Visual Studio che purtroppo non supporta più macro/scripting. –

2

Utilizzando il link Preet provided sono arrivato fino a questo che avvia il browser predefinito:

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

Sono arrivato con questo. Come dice, dovrai aggiungere un riferimento a "System.Web" per HttpUtility.UrlEncode. Inoltre, ci sono alcune "opzioni" nel codice commentate. Parti prese da http://www.codinghorror.com/blog/2005/10/google-search-vsnet-macro.html, ma molto migliorate (IMHO).

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 
Problemi correlati