Visual Studio Macro: Locate Item in Solution Explorer on Demand
来源:互联网 发布:串口转网络模块 编辑:程序博客网 时间:2024/05/02 10:20
From: http://dvanderboom.wordpress.com/2008/03/21/visual-studio-macro-track-item-in-solution-explorer-on-demand/
[ For an updated macro that works best with VS2008 and VS2010, seethe followup article. ]
By default, Visual Studio’s Solution Explorer will update its selected item based on the currently active document. This is occassionally useful when exploring unfamiliar code, but it’s annoying when it’s always on, bouncing around as you navigate through larger solutions.
Do you want to disable project item tracking?
Go to Tools—>Options, expand Projects and Solutions, selectGeneral, and then uncheck the box next to Track Active Item in Solution Explorer.
But what if you’re looking at a document, and you want to know where in Solution Explorer it’s located? Unfortunately, you would have to go back intoTools—>Options, …, turn that option back on, locate the project item, and then turn it back off again.
This is very inconvenient. What would be ideal is a button or hot key that would jump to that item in Solution Explorer just once, on demand. “Where’s the corresponding project item for this document? Find it and then don’t track after that.”
After about two hours of messing around with the VS SDK, trying to read the documentation, finding out that this particular option isn’t available for manipulation through automation, and resolving to figure out another way to do it, I finally accomplished this goal and I’m happy to share the macro that I created.
Don’t know how to create a macro?
Adding a macro is easy. First open the Macro Explorer. Press Alt-F8, or if you’ve remapped your keys, go toTools—>Macros—>Macro Explorer. You’ll probably have a project in there called MyMacros. If not, right-click on Macros and selectNew Macro Project… A window pops up, and you can enter a project name. Once you have selected or created a macro project, right click on the project name and selectNew module… I named mine Utilities. Right click on the module name and selectEdit. You should now see a VB code editor window.
Copy the following code into the editor and press Ctrl-S to save it.
Imports SystemImports EnvDTEImports EnvDTE80Imports EnvDTE90Public Module Utilities Public Sub TrackProjectItem() Dim solution As Solution2 = DTE.Solution If Not solution.IsOpen OrElse DTE.ActiveDocument Is Nothing Then Return solution.FindProjectItem(DTE.ActiveDocument.FullName).ExpandView() Dim FileName As String = DTE.ActiveDocument.FullName Dim SolutionExplorerPath As String Dim items As EnvDTE.UIHierarchyItems = DTE.ToolWindows.SolutionExplorer.UIHierarchyItems Dim item As Object = FindItem(items, FileName, SolutionExplorerPath) If item Is Nothing Then MsgBox("Couldn't find the item in Solution Explorer.") Return End If DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate() DTE.ActiveWindow.Object.GetItem(SolutionExplorerPath).Select(vsUISelectionType.vsUISelectionTypeSelect) End Sub Public Function FindItem(ByVal Children As UIHierarchyItems, ByVal FileName As String, ByRef SolutionExplorerPath As String) As Object For Each CurrentItem As UIHierarchyItem In Children Dim TypeName As String = Microsoft.VisualBasic.Information.TypeName(CurrentItem.Object) If TypeName = "ProjectItem" Then Dim projectitem As EnvDTE.ProjectItem = CType(CurrentItem.Object, EnvDTE.ProjectItem) Dim i As Integer = 1 While i <= projectitem.FileCount If projectitem.FileNames(i) = FileName Then SolutionExplorerPath = CurrentItem.Name Return CurrentItem End If i = i + 1 End While End If Dim ChildItem As UIHierarchyItem = FindItem(CurrentItem.UIHierarchyItems, FileName, SolutionExplorerPath) If Not ChildItem Is Nothing Then SolutionExplorerPath = CurrentItem.Name + "\" + SolutionExplorerPath Return ChildItem End If Next End FunctionEnd Module
Now let’s hook it up to a keyboard hot key to make it super fast to access. If you openTools—>Options, expand Environment, select Keyboard, and in the text box labeled Show commands containing, type part of the macro name, such asTrackProjectItem. You’ll see your new macro in the list highlighted. In the dropdown control labeledUse new shortcut in, select Text Editor. Click on the box that saysPress shortcut keys, and press a key combination. I chose Alt-T (for Track), which was available in that context to my surprise.
Try it out. Open up a few code or XML windows. Make one of those windows active and press your hot key. The item in Solution Explorer that corresponds to the current document window will activate.
- Visual Studio Macro: Locate Item in Solution Explorer on Demand
- Visual Studio: Track Active Item in Solution Explorer
- Visual Studio Macro View
- 折磨我的VS IDE文件自动定位功能(Track Active Item in Solution Explorer)
- VS IDE文件自动定位功能(Track Active Item in Solution Explorer)
- Connect to LocalDB in Visual Studio Server Explorer
- Visual Studio 2008 可扩展性开发(六):操作Solution Explorer
- Solution of Problems in Building sscli via Visual Studio 2008
- Visual Studio 2008 可扩展性开发(二):Macro和Add-In初探
- Visual Studio 2008 可扩展性开发(二):Macro和Add-In初探
- Visual Studio使用小技巧4 – Where am I(在Solution Explorer中显示当前文档)
- Visual Studio使用小技巧4 – Where am I(在Solution Explorer中显示当前文档)
- visual studio linker errors solution
- 使用宏(Macro)扩展Visual Studio IDE
- How to embed Lua in C++ on Visual Studio
- zz - Visual Studio trick to quickly find any file in solution
- undefined macro: AM_CONFIG_HEADER solution
- debugging on visual studio
- ARM汇编语言(6) ARM处理器模式及CPSR
- Older Blog’s Index - 姜江
- openssl/md5.h没有此文件
- HDU 1171 Big Event in HDU(多重背包)
- windowsphone7实现文件上传
- Visual Studio Macro: Locate Item in Solution Explorer on Demand
- Mysql存储过程的简单创建
- xml操作之xpath
- ubuntu安装投影仪后系统无法启动
- android+手机开发实用总结集锦
- Linux查看端口使用状态、关闭端口方法
- Linux查看端口使用状态及启动
- 如何在Linux中查看所有正在运行的进程
- HDU 2084 数塔 Python实现