开发VS2008 AddIn 入门Sample

本文主要介绍的是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

实时了解作者更多技术文章,技术心得,请关注微信公众号“轩脉刃的刀光剑影”

本文基于署名-非商业性使用 3.0许可协议发布,欢迎转载,演绎,但是必须保留本文的署名叶剑峰(包含链接http://www.cnblogs.com/yjf512/),且不得用于商业目的。如您有任何疑问或者授权方面的协商,请与我联系

原文地址:https://www.cnblogs.com/yjf512/p/1752401.html