代码改变世界

开发VS2008 AddIn 入门Sample

2010-06-05 21:10  轩脉刃  阅读(3974)  评论(4编辑  收藏  举报

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

环境要求:VS2008;.Net3.5

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

 

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

 

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

选择使用C#编写Addin

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

输入name和description

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

选择需要About Information

summary

 

 

Finish 生成solution

 

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

 

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

 

代码
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类中增加方法:

 

代码
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工具时候会发现文本已经创建,并且文字也已经加入到文本中:

 

至此,VS AddIn开发完成。

 

PS:本文示例参考:http://www.c-sharpcorner.com/uploadfile/mgold/addins11292005015631am/addins.aspx