VS平台插件开发,怎样将制作的插件加入到VS的右键菜单里面?

hyblusea 2009-04-14 11:35:37
新建的一个“外接程序”,默认是在工具菜单里面,我想把他加入到右键菜单里,该怎么弄呢?
...全文
688 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
jingzhongrong 2009-04-15
  • 打赏
  • 举报
回复
据我了解(可能并不准确),Connect类在每次调用都会被new,也就是我代码中使用InternalInfo这个类的原因,我使用它来保存添加的菜单。
你看看是不是这个原因
goeasy2005 2009-04-15
  • 打赏
  • 举报
回复
路过
hyblusea 2009-04-15
  • 打赏
  • 举报
回复
已经改了,仍然没有出现在右键菜单

还是结贴算了,谢谢jingzhongrong 的帮助 ,请给我一些关于插件开发的资料好吗?我对这方面不是很理解

现在实现的是在代码区单击右键,如果我想在其他地方的右键菜单插入一些命令又该怎么办呢?谢谢

我的邮箱:hyblusea@126.com 非常感谢 。
jingzhongrong 2009-04-15
  • 打赏
  • 举报
回复
QueryStatus和Exec中的Mytest1.Connect.Mytest1修改成Mytest1.Connect.SearchOnGoogle
hyblusea 2009-04-15
  • 打赏
  • 举报
回复
我也加入了InternalInfo这个类,而且使用的代码和你是一样的。

using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Windows.Forms;

namespace Mytest1
{
/// <summary>用于实现外接程序的对象。</summary>
/// <seealso class='IDTExtensibility2' />
public class Connect : IDTExtensibility2, IDTCommandTarget
{

private DTE2 _applicationObject;
private AddIn _addInInstance;

/// <summary>实现外接程序对象的构造函数。请将您的初始化代码置于此方法内。</summary>
private InternalInfo internalInfo = InternalInfo.GetInternalInfoInstance();

public Connect()
{
}

/// <summary>实现 IDTExtensibility2 接口的 OnConnection 方法。接收正在加载外接程序的通知。</summary>
/// <param term='application'>宿主应用程序的根对象。</param>
/// <param term='connectMode'>描述外接程序的加载方式。</param>
/// <param term='addInInst'>表示此外接程序的对象。</param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if (connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object[] contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;


// 插件命令到快捷菜单
CommandBars commandBars = ((CommandBars)_applicationObject.CommandBars);
CommandBar commandBar = (CommandBar)commandBars["Code Window"];

this.internalInfo.Pop = (CommandBarPopup)(CommandBarControl)commandBar.Controls.Add( MsoControlType.msoControlPopup, 1, null, 1, true );
this.internalInfo.Pop.Caption = "SearchOnWeb"; //菜单项显示内容
this.internalInfo.Pop.Enabled = true; //可用
this.internalInfo.Pop.Visible = true; //可见
this.internalInfo.Pop.BeginGroup = true;//添加分隔线

try
{
Command searchGoogle = commands.AddNamedCommand2(
_addInInstance,
"SearchOnGoogle",
"&Google...",
"Search on Google",
true,
0,
ref contextGUIDS,
(int)vsCommandStatus.vsCommandStatusSupported +
(int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStylePictAndText,
vsCommandControlType.vsCommandControlTypeButton
);
searchGoogle.AddControl(this.internalInfo.Pop.CommandBar, 1);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}

/// <summary>实现 IDTExtensibility2 接口的 OnDisconnection 方法。接收正在卸载外接程序的通知。</summary>
/// <param term='disconnectMode'>描述外接程序的卸载方式。</param>
/// <param term='custom'>特定于宿主应用程序的参数数组。</param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
{
}

/// <summary>实现 IDTExtensibility2 接口的 OnAddInsUpdate 方法。当外接程序集合已发生更改时接收通知。</summary>
/// <param term='custom'>特定于宿主应用程序的参数数组。</param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref Array custom)
{
}

/// <summary>实现 IDTExtensibility2 接口的 OnStartupComplete 方法。接收宿主应用程序已完成加载的通知。</summary>
/// <param term='custom'>特定于宿主应用程序的参数数组。</param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref Array custom)
{
}

/// <summary>实现 IDTExtensibility2 接口的 OnBeginShutdown 方法。接收正在卸载宿主应用程序的通知。</summary>
/// <param term='custom'>特定于宿主应用程序的参数数组。</param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref Array custom)
{
}

/// <summary>实现 IDTCommandTarget 接口的 QueryStatus 方法。此方法在更新该命令的可用性时调用</summary>
/// <param term='commandName'>要确定其状态的命令的名称。</param>
/// <param term='neededText'>该命令所需的文本。</param>
/// <param term='status'>该命令在用户界面中的状态。</param>
/// <param term='commandText'>neededText 参数所要求的文本。</param>
/// <seealso class='Exec' />
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
{
if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if (commandName == "Mytest1.Connect.Mytest1")
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
return;
}
}
}

/// <summary>实现 IDTCommandTarget 接口的 Exec 方法。此方法在调用该命令时调用。</summary>
/// <param term='commandName'>要执行的命令的名称。</param>
/// <param term='executeOption'>描述该命令应如何运行。</param>
/// <param term='varIn'>从调用方传递到命令处理程序的参数。</param>
/// <param term='varOut'>从命令处理程序传递到调用方的参数。</param>
/// <param term='handled'>通知调用方此命令是否已被处理。</param>
/// <seealso class='Exec' />
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if (commandName == "Mytest1.Connect.Mytest1")
{


TextDocument textDoc = (TextDocument)this._applicationObject.ActiveDocument.Object("TextDocument");
EditPoint editPoint = textDoc.Selection.ActivePoint.CreateEditPoint(); // 获取当前文档光标所在位置
EditPoint endPoint = textDoc.EndPoint.CreateEditPoint();

Form1 frm = new Form1();
frm.ShowDialog();
editPoint.Insert(PublicVar.sCode); // 在光标位置插入字符串
endPoint.EndOfDocument();
editPoint.StartOfDocument();
editPoint.SmartFormat(endPoint);

handled = true;
return;
}
}
}

//private DTE2 _applicationObject;
//private AddIn _addInInstance;
}
}


这是我的代码
jingzhongrong 2009-04-14
  • 打赏
  • 举报
回复
jingzhongrong 2009-04-14
  • 打赏
  • 举报
回复

你需要获取对应窗口的CommandBar对象,比如代码编辑器:
CommandBars commandBars = ((CommandBars)_applicationObject.CommandBars);
CommandBar commandBar = (CommandBar)commandBars["Code Window"];

可以参考http://download.csdn.net/source/1205140,
效果:http://p.blog.csdn.net/images/p_blog_csdn_net/jingzhongrong/EntryImages/20090414/1.JPG
mykelly6 2009-04-14
  • 打赏
  • 举报
回复
tool的add_in manager里面不能设置吗?
我记得之前自己弄了个简单的,是设置了快捷键的
「已注销」 2009-04-14
  • 打赏
  • 举报
回复
帮顶,学习。
cja03 2009-04-14
  • 打赏
  • 举报
回复
帮顶,学习。

怎么近来这么多人做VS的插件开发??
hyblusea 2009-04-14
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jingzhongrong 的回复:]
在QueryStatus中有没有相应处理代码显示菜单
[/Quote]

这是我的代码:

public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
{
if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if (commandName == "Mytest1.Connect.Mytest1")
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
return;
}
}
}



这是您的代码:

public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText,
ref vsCommandStatus status, ref object commandText)
{
if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if (commandName == "SearchGoogle.Connect.SearchOnGoogle")
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported |
vsCommandStatus.vsCommandStatusEnabled;
return;
}
}
}


没什么不一样的地方吧,除了名称空间
jietuan 2009-04-14
  • 打赏
  • 举报
回复
jingzhongrong 2009-04-14
  • 打赏
  • 举报
回复
在QueryStatus中有没有相应处理代码显示菜单
hyblusea 2009-04-14
  • 打赏
  • 举报
回复
TO jingzhongrong :

我用可用分换了下载资源分,我把你的代码嵌到我的工程,但是在快捷菜单里面仍然没有显示出来,不知道是怎么回事,再帮帮忙。
hyblusea 2009-04-14
  • 打赏
  • 举报
回复
可以贴代码看看吗?
我在网上也找过一些代码。但是在快捷菜单里面都看不到。另外我没有下载分了。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧