秋梦无痕

一场秋雨无梦痕,春夜清风冻煞人。冬来冷水寒似铁,夏至京北蟑满城。

Avatar

在VS Studio .Net中添加Google搜索功能

from: blog@little-garins

首先,用tlbimp.exe生成一个SHDocVw COM Type Library的.NET可以访问的dll:
tlbimp c:\Windows\system32\shdocvw.dll /out:"c:\Program Files\Microsoft Visual Studio .NET\Common7\IDE\PublicAssemblies\Interop.shdocvw.dll"

其次,创建一个宏项目,比如说,ifyr,修改宏名,MacroUtils,然后编辑那个MacroUtils(双击或者右键菜单中选编辑), 会出现宏编辑器。

然后,给项目添加引用:Interop.SHDocVw和System.Web。

接下来,清除MacroUtil宏中的内容,再把下面的代码拷贝过去:

Imports EnvDTE
Imports Interop

Public Module MacroUtils
Sub GoogleSearch()
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 <= 0 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 interfcace
Dim br As shdocvw.WebBrowser
br = CType(win.Object, shdocvw.WebBrowser)

'// Do The Navigation
br.Navigate(url, Nothing, Nothing, Nothing, Nothing)
End Sub
End Module

关闭宏编辑器。到.Net环境中选择菜单"工具-->选项",在"环境->键盘"中输入google,定位到"Macros.ifyr.MacroUtils.GoogleSearch",想用什么键就随便你了,比如说,Alt + F1。
好,大功告成,打开一个文件,选上几个字母,按下Alt + F1……今天你google了没有?

评论已关闭