注册自己的控件到Visual Studio 环境里

原文: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;
    
ifthis._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];
    
forint 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
    
{
        
forint 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;

            
forint 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 + ".");
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值