2008年9月25日星期四

Track Active Item in Solution Explorer

我现在所在的项目规模很大,在solution中的源文件就有几千个。平时编辑的时候要全部展开是不可能的,特别是对于我这样的新进员工,一下子接触到这么大的Project还真有点摸不着头脑。

这几天我发现一个问题;当我在编辑某个文件的时候,我很希望看到Solution Explorer同时帮我定位到那个文件,这样我可以看到该文件在项目中的哪个项目的哪个子目录之下,这有助于我了解程序的逻辑结构。同时也因为这个项目中的文件的物理结构和逻辑结构完全对应不上,使我没法通过物理结构直接映射到逻辑结构(也就是在Solution Explorer中的树结构)。因此,Track Active Item这个功能对我而言很有必要了。然而,当默认安装好VS2005之后,这个功能是没有的。

于是我很愚蠢的花了3个多小时时间,研究了VS2005的宏编程,写了一个宏,当我选中某个文件的时候,Solution Explorer会自动定位到那个文件,必要时会自动展开。还洋洋得意的把这个宏发给同事。结果,同事告诉我,在VS2005中早已有这样的设置,我这是在重新发明轮子的傻事。

VS2005中的设置在Tools->Option里面,如下图



另外,这里也附上我写的宏,以纪念这次愚蠢的行动。


Public Sub LocateFileInSolutionExplorer()

' Get the UIHierarchy
Dim oUih As UIHierarchy = DTE.ToolWindows.SolutionExplorer

' Check if there is solution exists
If (oUih.UIHierarchyItems.Count = 0) Then
Return
End If

' Get the top level item in UIHierarchy.(solution)
Dim oUihSln As UIHierarchyItem = oUih.UIHierarchyItems.Item(1)

' Reverse finding corressponding parent until solution node.
Dim oItem As Object = DTE.ActiveDocument.ProjectItem
Dim oPath As String = oItem.Name

While Not (TypeOf oItem Is Project)
oItem = oItem.Collection.Parent
oPath = oItem.Name & "\" & oPath
End While

'Construct item path
oPath = oUihSln.Name & "\" & oPath
'Select this file
oUih.GetItem(oPath).Select(vsUISelectionType.vsUISelectionTypeSelect)
'Activate the solution window so the selected file is hilighted
DTE.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Activate()

End Sub




这个事情给我的教训是,当遇到问题时,首先问问周围的人,然后Google一下,再去Usenet问一下,最后才自己动手。除非是想学习某样技术,否则没有必要浪费时间重新发明轮子。

没有评论: