Udostępnij za pośrednictwem


Visual Studio Add-in: Use Vista Search directly from Visual Studio

I have used Windows Vista from last November and now I have noticed that I'm really heavy user of search. Before Vista I had a lot of problems finding my stuff because I just couldn't remember where I have saved my files. I used my own directory structures that would help me to find files easier. But still sometimes I just couldn't find them. Vista however makes this problem go away. I'll just press Windows key and start typing something relating to the item I'm looking for. And somehow my computer finds the correct results and really really fast too.

But few days ago I had and interesting idea. How could I bring that Vista search into my favorite tool: Visual Studio. So I decided to create Visual Studio Add-in that creates the query for me. I created new project with Add-in template:
Visual Studio: New project dialog 
Then I added following codes to the generated class:

 1  [DllImport("user32.dll", EntryPoint = "keybd_event")]  2  public static extern void KeyboardEvent(uint bVk,  3     uint bScan, uint dwFlags, uint dwExtraInfo);  4   5  public const uint VK_LWIN = 0x5B;  6  public const uint KEYEVENTF_EXTENDEDKEY = 0x01; 7  public const uint KEYEVENTF_KEYUP = 0x02;  8   9  public void Exec(String commandName, 10                    vsCommandExecOption executeOption, 11                   ref object varIn, ref object varOut, ref bool handled) 12  { 13    handled = false; 14    if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault) 15    { 16      if (commandName == "VSExplorerAddin.Connect.VSExplorerAddin") 17      { 18        handled = true; 19        EnvDTE.TextSelection selection = null; 20        try 21        { 22          selection = _applicationObject.ActiveDocument.Selection 23            as EnvDTE.TextSelection; 24          selection.GotoLine(selection.CurrentLine, false); 25          selection.EndOfLine(true); 26          String selectedText = selection.Text.Trim(); 27          selection.Text = ""; 28  29          KeyboardEvent(VK_LWIN, 0, 0, 0); 30          KeyboardEvent(VK_LWIN, 0, KEYEVENTF_KEYUP, 0); 31          SendKeys.Send(selectedText + "{ENTER}");32        `` } 33        catch (Exception ex) 34        { 35          if (selection != null) 36          { 37            selection.Text = ex.ToString(); 38          } 39          else 40          { 41            MessageBox.Show(ex.ToString()); 42          } 43        } 44        return; 45      } 46    }47  }

Most of the stuff is put into the Exec-method, but added also KeyboardEvent (on lines 1 to 3) which is used to send the Windows key press. I needed that because I couldn't find way to send windows key press using the SendKeys. If that would be possible, it would just simplify this code a little bit more (so if you know better way then please let me know :-). But anyway the actual code is pretty simple. On lines 22 to 27 the current selection is copied into local variable and then the selection is cleared. And lines 29 and 30 just send the win key and finally on line 31 the previously stored selection is typed into the search box. And if something goes wrong (=exception is thrown) then we're setting the selection to have the error message or we'll just popup  a simple messagebox if selection is unavailable.

Do you want to see that stuff in action? Even if you don't I'm still going to show it to you :-) So first I'll just press F5 to start the project. Visual Studio actually launches new Visual Studio which can be used for testing my new addin. Then I just open any project of mine and wait it to be loaded. After that I can just type in my search word which is on line 16 (in this case word).

After I have typed in my search word I can launch my magical addin. In this case I'll use Alt-T (which opens Tools menu).
 
And when menu is open I'll just hit the Enter (since the addin is the first choice). After that my addin launches word as a search. Vista opens the first search result automatically because our addin sends Enter as last key press. As an end result we have Microsoft Office Word 2007 running:

This sample shows that you can easily add more functionality to Visual Studio. In this case I just added my favorite feature from Vista :) I hope you start creating you own addins rightaway! It's not that hard... believe me. Of course this could be done as a Macro as well, but I'll leave that stuff to another time.

Anyways... happy hacking!

J

Comments

  • Anonymous
    March 19, 2007
    The comment has been removed

  • Anonymous
    April 13, 2007
    Thank you! [url=http://bguyjfvb.com/ddbx/trbw.html]My homepage[/url] | [url=http://atezmifj.com/hxxy/yegc.html]Cool site[/url]

  • Anonymous
    April 13, 2007
    Well done! <a href="http://bguyjfvb.com/ddbx/trbw.html">My homepage</a> | <a href="http://udufyljx.com/mzdy/fvbf.html">Please visit</a>

  • Anonymous
    April 13, 2007
    Thank you! http://bguyjfvb.com/ddbx/trbw.html | http://rjvbjcij.com/fvtm/cpey.html

  • Anonymous
    May 12, 2007
    MSN I NIIPET <a href="http://msn.com">MSN</a>

  • Anonymous
    August 01, 2007
    <a href="httpwwwdhcgpdiacnpage45html">abcseniormiddleeastcorrespondent</a> abcseniormiddleeastcorrespondent,<a href="httpwwwdhcgpdiacnpage64html">vb6getwindowsfolder</a> vb6getwindowsfolder,<a href="httpwwwdhcgpdiacnpage69html">jetairairwayslogobag</a> jetairairwayslogobag,<a href="httpwwwdhcgpdiacnpage69html">usairwaysdividendmilesvisasignaturecard</a> usairwaysdividendmilesvisasignaturecard,<a href="httpwwwdhcgpdiacnpage56html">egforumsgtdaviddrake</a> egforumsgtdaviddrake,<a href="httpwwwdhcgpdiacnpage63html">checkeredflagdie-cast</a> checkeredflagdie-cast,<a href="httpwwwdhcgpdiacnpage62html">harmlessdeception</a> harmlessdeception,<a href="httpwwwdhcgpdiacnpage66html">harveyfenton</a> harveyfenton,<a href="httpwwwdhcgpdiacnpage65html">photoshopelementsslideshow</a> photoshopelementsslideshow,<a href="httpwwwdhcgpdiacnpage56html">wwwegsparkcomtr</a> wwwegsparkcomtr,

  • Anonymous
    August 01, 2007
    <a href="httpwwwqrqplfsbcnpage49html">circleteachertelevisionseriesblackboard</a> circleteachertelevisionseriesblackboard,<a href="httpwwwqrqplfsbcnpage46html">howlingiiithemarsupialsimdb</a> howlingiiithemarsupialsimdb,<a href="httpwwwqrqplfsbcnpage41html">genresinlibraries</a> genresinlibraries,<a href="httpwwwqrqplfsbcnpage45html">clementefieldnewyork</a> clementefieldnewyork,<a href="httpwwwqrqplfsbcnpage50html">presariov2000batteries</a> presariov2000batteries,<a href="httpwwwqrqplfsbcnpage42html">michaeljordanlastshotphoto</a> michaeljordanlastshotphoto,<a href="httpwwwqrqplfsbcnpage35html">musiccomposedbydavidholmesinoceanstwelve</a> musiccomposedbydavidholmesinoceanstwelve,<a href="httpwwwqrqplfsbcnpage37html">johnsonjb-106</a> johnsonjb-106,<a href="httpwwwqrqplfsbcnpage55html">middleagebrewsters</a> middleagebrewsters,<a href="httpwwwqrqplfsbcnpage55html">summervacationrentalsandlongpondandbrewster</a> summervacationrentalsandlongpondandbrewster,

  • Anonymous
    October 05, 2007
    Hi, Came across the article. Had a quick question though? Is there a way by which I can create an Add-In to catch key presses in Visual Studio. Thanks! Sasha

  • Anonymous
    October 24, 2007
    Hi Sasha! Actually I don't believe that you can do that easily. Of course there are ways to start listening keyboard presses to achieve some kind of fancy features but you need to be careful not to make it spend too many CPU cycles on your custom feature. What is that you're trying to achieve? J

  • Anonymous
    May 22, 2008
    This is the list of the Visual Studio Add-ins, most of which are open source or free. I hope this list

  • Anonymous
    July 29, 2009
    Недорогая установка окон пвх, установка деревянных окон, технология установки окон, установка окон в деревянном доме, инструкция по установке окон, технология установки пластиковых окон, фирмы по установке окон, установка окон цена, договор на установку окон, установка решеток на окнах, установка решеток на окна, установка металлопластиковых окон, установка окон самостоятельно, установка мансардных окон, установка окон своими руками, инструкция по установке пластиковых окон

  • Anonymous
    August 01, 2009
    The comment has been removed

  • Anonymous
    August 04, 2009
    The comment has been removed