原文:http://www.cnblogs.com/happyy2k/archive/2004/11/29/70401.html
1. 首先要获得IDE的DTE 对象(Get DTE's Object)
if
(
this
._env
==
null
)

{
Type latestDTE = Type.GetTypeFromProgID("VisualStudio.DTE");
if( latestDTE == null )
throw new System.ApplicationException("Can not get DTE Type from VisualStudio.DTE ProgID.");

this._env = Activator.CreateInstance(latestDTE) as EnvDTE.DTE;
if( this._env == null )
throw new System.ApplicationException("Fail to create DTE instance.");
}
2. 增加自己的Tab到Tools Box 里(Add new Tab to Tools Box)
//
Get the main window of ToolBox;
Window ToolBoxMainWin;
ToolBoxMainWin
=
this
._env.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
if
( ToolBoxMainWin
==
null
)
throw
new
System.ApplicationException(
"
Can not get the ToolBox window.
"
);

//
Get the ToolBox Object
ToolBox TheToolBox;
TheToolBox
=
(ToolBox) ToolBoxMainWin.Object;

//
Get the ToolBox Tab collection
ToolBoxTabs TheToolBoxTab;
TheToolBoxTab
=
TheToolBox.ToolBoxTabs;

//
Check the old tab
ToolBoxTab TheTab
=
null
;
foreach
( ToolBoxTab oldTab
in
TheToolBox.ToolBoxTabs )

{
if( oldTab.Name != null && oldTab.Name == strTabName )

{
TheTab = oldTab;
break;
}

}
//
Tab is not found, add the new one.
if
( TheTab
==
null
)
TheTab
=
TheToolBoxTab.Add(strTabName);

if
( TheTab
==
null
)
throw
new
System.ApplicationException(
"
Adding new ToolBox Tab:
"
+
strTabName
+
"
fails.
"
);

//
win bug - need to do this for some reason
this
._env.ExecuteCommand(
"
View.PropertiesWindow
"
,
""
);
3. 把自己的编译好的Assembly 加入到新Tab 中(Add compiled Assembly to the new Tab)
//
Active the tab
TheTab.Activate();

//
Add Our Components in the Assembly to the tab
TheTab.ToolBoxItems.Add(
""
,
strAssemblyName,
vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
4. 同理,如果要删除自己的控件,则需要在Tab里查询到控件,然后删除,如果发现Tab内没有任何控件也可以选择删除这个Tab(Same concept, if you want delete custmer control from IDE, need search the control first, then delete it , if the Tab don't have any other control, also can choose delete the Tab)
//
tab item count is 1(pointer) or no item name specified, delete the tab immediately
if
( theTab.ToolBoxItems.Count
==
1
||
toolBoxItemNames
==
null
||
toolBoxItemNames.Length
==
0
)
theTab.Delete();
else

{
// Remove the items
ToolBoxItem OldItem;
System.Collections.ArrayList deleteItems = new System.Collections.ArrayList();
bool[] deletedIndex = new bool[toolBoxItemNames.Length];
for( int i = 0; i < toolBoxItemNames.Length; i++ )
deletedIndex[i] = false;

System.Collections.IEnumerator ItemEnum = theTab.ToolBoxItems.GetEnumerator();
ItemEnum.Reset();
while( ItemEnum.MoveNext() )

{
OldItem = (ToolBoxItem)ItemEnum.Current;
bool itemFound = false;

if( OldItem != null )

{
for(int i = 0; i < toolBoxItemNames.Length; i++)

{
if( deletedIndex[i] )
continue;

if( toolBoxItemNames[i] == null && OldItem.Name == null )

{
itemFound = true;
deletedIndex[i] = true;
break;
}
if( toolBoxItemNames[i] != null && OldItem.Name != null && toolBoxItemNames[i] == OldItem.Name )

{
itemFound = true;
deletedIndex[i] = true;
break;
}
}
}
if(!itemFound)
continue;

deleteItems.Add(OldItem);
}
if( deleteItems.Count == ( theTab.ToolBoxItems.Count - 1) ) // delete item == toolitems -1 (pointer), delete the tab directly.
theTab.Delete();
else

{
for( int i = 0; i < deleteItems.Count; i++ )

{
OldItem = (ToolBoxItem)deleteItems[i];
// delete the item.
OldItem.Delete();
}
if( theTab.ToolBoxItems.Count == 1 )
theTab.Delete();
// checked undeleted items.
if( deleteItems.Count != toolBoxItemNames.Length )

{
string undeletedNames = "";
bool first = true;

for( int i = 0; i < toolBoxItemNames.Length; i++ )

{
if( deletedIndex[i] )
continue;
if( first )

{
undeletedNames = undeletedNames + toolBoxItemNames[i];
first = false;
}
else
undeletedNames = undeletedNames + "," + toolBoxItemNames[i];

}

throw new System.ApplicationException("Fail to remove item: " + undeletedNames + ".");
}
}
}
2. 增加自己的Tab到Tools Box 里(Add new Tab to Tools Box)
3. 把自己的编译好的Assembly 加入到新Tab 中(Add compiled Assembly to the new Tab)
4. 同理,如果要删除自己的控件,则需要在Tab里查询到控件,然后删除,如果发现Tab内没有任何控件也可以选择删除这个Tab(Same concept, if you want delete custmer control from IDE, need search the control first, then delete it , if the Tab don't have any other control, also can choose delete the Tab)
本文围绕.Net学习,介绍如何将自定义控件注册到Visual Studio环境。首先获取IDE的DTE对象,接着在工具框添加新标签,再将编译好的Assembly加入新标签。同时也说明了删除自定义控件的方法,若标签内无控件可删除标签。
4473

被折叠的 条评论
为什么被折叠?



