Visual Studio.Net 打开一个工程的时候,有时候子项目很多,而且你有不是很熟悉这个项目。
然后你查找某个关键字,打开一个文件,你需要知道它在Solution Explorer中属于哪里,也就是高亮这个文件节点,
你应该怎么做?这里提供一个办法供你参考。
我们知道 Visual Studio.Net 提供了可扩展性开发的功能,也就是插件功能,比较有名的是Visual AssistX
另外有很多文章介绍如何开发插件,
http://www.cnblogs.com/anderslly/archive/2009/02/25/first-macro-addin.html
http://www.cnblogs.com/anderslly/archive/2009/02/28/vs-addin-explained-part1.html
http://www.cnblogs.com/anderslly/archive/2009/03/03/vs-addin-explained-part2.html
我的方法是先得到就是从SolutionExplorer的Item中一个个找当前的ProjectItem,然后高亮,很简单。
相关代码如下,请指教
1
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
2
{
3
handled = false;
4
if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
5
{
6
if(commandName == "SyncAddin.Connect.SyncAddin")
7
{
8
DoSync();
9
handled = true;
10
return;
11
}
12
}
13
}
14
15
private bool bFound;
16
public void DoSync()
17
{
18
//_applicationObject.ActiveDocument.ProjectItem.ExpandView();
19
20
ProjectItem projectitem = _applicationObject.ActiveDocument.ProjectItem;
21
22
UIHierarchy UIH = _applicationObject.ToolWindows.SolutionExplorer;
23
UIH.Parent.SetFocus();
24
25
// Requires a reference to System.Text.
26
// Set a reference to the first level nodes in Solution Explorer.
27
// Automation collections are one-based.
28
UIHierarchyItem UIHItem = UIH.UIHierarchyItems.Item(1);
29
30
bFound = false;
31
RecursiveFindItem(projectitem, UIHItem);
32
}
33
34
private void RecursiveFindItem(ProjectItem projectItem, UIHierarchyItem item)
35
{
36
if (bFound == true) return;
37
if (projectItem == null) return;
38
if (item == null) return;
39
40
if (projectItem.Name == item.Name)
41
{
42
item.Select(vsUISelectionType.vsUISelectionTypeSelect);
43
bFound = true;
44
return;
45
}
46
47
foreach (UIHierarchyItem subitem in item.UIHierarchyItems)
48
RecursiveFindItem(projectItem, subitem);
49
}
50

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

整个项目(for vs 2008) 从这里下载:/Files/kuaishou/SyncAddin.7z