Visual Studio Hacks [Electronic resources]

Andrew Lockhart

نسخه متنی -صفحه : 172/ 94
نمايش فراداده

Hack 61. Redirect Help to Google

Supercharge Visual Studio's help by hooking up a macro to Google and other search engines.

[Hack #60], you saw a way to highlight text and then query a search engine using that selected text. This hack is similar in that it allows you to select text and then search Google with that text. The difference between these hacks is that this hack uses a macro. This makes it easier to wire up to a shortcut key and to change the search engine to use.

The first step to using this macro is creating a managed class to interact with from the macro. Here is the process to do this:

Open the Visual Studio .NET 2003 command prompt under Start Programs Visual Studio .NET 2003 Visual Studio .NET Tools.

Enter the following command into the command prompt:

tlbimp %SystemRoot%\system32\shdocvw.dll /out:"%VSINSTALLDIR%\PublicAssembliesInterop.hdocvw.dll"

Next, you will need to create the actual macrofor detailed instructions on how to create macros, please refer to [Hack #51] :

Add a reference to your macro project to the Interop.SHDocVW assembly, which will be listed under the .NET tab.

Add a reference to the System.Web assembly.

Add the following line of code to the top of your module:

Imports Interop

Add the following code to the body of your module:

Sub DoGoogleSearch( )
Dim sel As TextSelection
Dim selectedText As String
Try
sel = DTE.ActiveDocument.Selection
selectedText = sel.Text
Catch ex As System.Exception
selectedText = String.Empty
End Try
If selectedText.Trim( ).Length < 1 Then
'// Throw UI to get Text
Dim criteria As String = InputBox("Enter Search Criteria")
If (criteria.Trim( ).Length = 0) Then
Exit Sub
Else
selectedText = criteria
End If
End If
Dim url As String = _
String.Format("http://www.google.com/search?hl=en&ie=UTF-8&q={0}", _
System.Web.HttpUtility.UrlEncode(selectedText))
'// Get the WebBrowser/Help Window
Dim win As Window
win = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindWebBrowser)
win.Visible = True
'// Get the interface
Dim br As SHDOCVW.WebBrowser
br = CType(win.Object, SHDOCVW.WebBrowser)
'// Do The Navigation
br.Navigate(url, Nothing, Nothing, Nothing, Nothing)
End Sub

The next step is to add a shortcut key or toolbar item for your macro. For information on performing either of these actions, please refer to [Hack #51].

Once your macro is created and accessible, you will be able to select a bit of text and then press your shortcut key (I like to assign Alt-F1 for this macro), and you will be shown a search results screen for that bit of code, as shown in Figure 7-13.

Figure 7-13. Google help

The nice thing about this being a macro is that it is so easy to change which search engine this macro uses. If you wanted to create a macro to search Google groups instead of Google web, you would simply need to copy this macro and change the following line from:

Dim url As String = _
String.Format("http://www.google.com/search?hl=en&ie=UTF-8&q={0}", _
System.Web.HttpUtility.UrlEncode(selectedText))

to the following:

Dim url As String = _
String.Format("http://groups.google.com/groups?q={0}", _
System.Web.HttpUtility.UrlEncode(selectedText))

As you can see from this example, it would be very simple to change this to any online resource that has a predictable query string format.

Thanks to Marty Garins for writing this macro and posting it on his web log, which can be found http://www.little-garins.com/Blogs/marty.