本文主要介绍的是VS2008插件开发

环境要求:VS2008;.Net3.5

目标:开发插件功能为“在VS中创建文本文档,并在文本开头输入//This code was created For Testing”


1,Create new project(Visual Studio Add-In)

 开发VS2008 AddIn 入门Sample_c#

2,按照wizard一步一步操作:

选择使用C#编写Addin

选择在.NET IDE 和Macro IDE中都可以使用AddIn

输入name和description

开发VS2008 AddIn 入门Sample_c#_02

选中确定需要AddIn在Tool中显示

开发VS2008 AddIn 入门Sample_.net_03

选择需要About Information

summary

 开发VS2008 AddIn 入门Sample_c#_04


Finish 生成solution

开发VS2008 AddIn 入门Sample_c#_05


此时debug(F5)这个solution,会跳出另外一个VS2008窗口,并且你会发现在Tool工具栏下有^-^笑脸:

开发VS2008 AddIn 入门Sample_.net_06


3 将Connect类中的Exec方法改为:


开发VS2008 AddIn 入门Sample_c#_07开发VS2008 AddIn 入门Sample_.net_08代码

public void Exec(string commandName, EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if ( executeOption ==
EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "CopyrRightAddIn.Connect.CopyrRightAddIn")
{
// Add your command execution here
CreateNewFile();
handled = true;
return;
}
}
}



4 在connect类中增加方法:


开发VS2008 AddIn 入门Sample_c#_07开发VS2008 AddIn 入门Sample_.net_08代码

public void CreateNewFile()
{
//Create a new text document.
_applicationObject.ItemOperations.NewFile("General\\Text File", "",
Constants.vsViewKindCode);
//Get a handle to the new document.
TextDocument objTextDoc = (TextDocument)_applicationObject.ActiveDocument.Object("TextDocument");
EditPoint objEditPoint = (EditPoint)objTextDoc.StartPoint.CreateEditPoint();
//Create an EditPoint and add some text.
objEditPoint.Insert("//This code was created For Testing");
}



5 build and debug

当按下Tool中的AddIn工具时候会发现文本已经创建,并且文字也已经加入到文本中:

开发VS2008 AddIn 入门Sample_c#_11


至此,VS AddIn开发完成。