在VisualStudio里建立 VisualStudio外接程序。
2015-04-15 10:54:19 访问(1622) 赞(0) 踩(0)
菜单主要的工作方法
OnConnection QueryStatus Exec
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
connectMode: 枚举,运行模式
ext_cm_AfterStartup 外接程序是在应用程序启动后加载的。
ext_cm_Startup 启动时加载的。
ext_cm_UISetup 自安装后首次被启动。
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
neededText: 枚举,指定是否返回检查信息
vsCommandStatusTextWantedNone 不返回信息。
vsCommandStatusTextWantedName 返回命令名。
vsCommandStatusTextWantedStatus 返回命令状态。
status: 枚举,当前状态
vsCommandStatusUnsupported 命令在此上下文中不受支持。
vsCommandStatusSupported 命令在此上下文中受支持。
vsCommandStatusEnabled 启用状态。
vsCommandStatusLatched 锁存状态。
vsCommandStatusNinched 保留。
vsCommandStatusInvisible 隐藏状态。
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
executeOption: 枚举
vsCommandExecOptionDoDefault 执行默认行为,无论是否提示用户输入。
vsCommandExecOptionPromptUser 不提示用户就执行命令。
vsCommandExecOptionDoPromptUser 获取用户输入后执行命令。
vsCommandExecOptionShowHelp 如果存在相应命令的帮助,则显示它,但不执行该命令。
我们先建立个静态类来帮我们描述菜单的问题 图形的信息
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;
namespace DSKJAddIn.DrawFactory
{
public class BeginData
{
public static ToolsData[] Tool = new ToolsData[]
{
new ToolsData("设置流转", "FlowCommand", 43, "DSKJ.Command.FlowCommand",""),
new ToolsData("编辑模型", "EditCommand", 44, "DSKJ.Command.EditCommand",""),
new ToolsData("添加模型", "AddCommand", 52, "DSKJ.Command.AddCommand","")
};
public static DTE2 ApplicationObject;
public static AddIn AddInInstance;
public class ToolsData
{
private string m_ViewText = "";
private string m_Command = "";
private int m_ImageIndex = 0;
private string m_ClassName = "";
private string m_Tip = "";
public ToolsData(string p_ViewText, string p_Command, int p_ImageIndex, string p_ClassName,string p_Tip)
{
m_ViewText = p_ViewText;
m_Command = p_Command;
m_ImageIndex = p_ImageIndex;
m_ClassName = p_ClassName;
m_Tip = p_Tip;
}
/// <summary>
/// 返回显示名称
/// </summary>
public string ViewText { get { return m_ViewText;} }
/// <summary>
/// 返回命令
/// </summary>
public string Command { get { return m_Command; } }
/// <summary>
/// 返回图形索引
/// </summary>
public int ImageIndex { get { return m_ImageIndex; } }
/// <summary>
/// 返回类名称
/// </summary>
public string ClassName { get { return m_ClassName; } }
/// <summary>
/// 标签名字
/// </summary>
public string Tip { get { return m_Tip; } }
}
}
}
建立一个添加菜单类和具体菜单调用的类
using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Windows.Forms;
namespace DSKJAddIn.DrawFactory
{
public class Factory
{
public static void AddMenu(Commands2 p_Command, CommandBarPopup p_ToolsPopup)
{
object[] _ContextGUIDS = new object[] { };
foreach (DrawFactory.BeginData.ToolsData OneData in DrawFactory.BeginData.Tool)
{
Command _Command = p_Command.AddNamedCommand2(BeginData.AddInInstance, OneData.Command, OneData.ViewText, OneData.Tip, true, OneData.ImageIndex, ref _ContextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
if ((_Command != null) && (p_ToolsPopup != null))
{
_Command.AddControl(p_ToolsPopup.CommandBar, 1);
}
}
}
public static void RunMenu(string p_Command, ref bool p_Handled)
{
foreach (DrawFactory.BeginData.ToolsData OneData in DrawFactory.BeginData.Tool)
{
if ("DSKJAddIn.Connect." + OneData.Command == p_Command)
{
MessageBox.Show(OneData.Command);
p_Handled = true;
return;
}
}
}
public static void RunCommand(string p_Command, ref vsCommandStatus status)
{
foreach (DrawFactory.BeginData.ToolsData OneData in DrawFactory.BeginData.Tool)
{
if ("DSKJAddIn.Connect." + OneData.Command == p_Command)
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
return;
}
}
}
}
}
改动OnConnection方法为,这里要注意的是 toolsControl = menuBarCommandBar.Controls["代码生成"];
如果当前VS没有直接就报错`~不会返回null
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
BeginData.ApplicationObject = (DTE2)application;
BeginData.AddInInstance = (AddIn)addInInst;
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
Commands2 commands = (Commands2)BeginData.ApplicationObject.Commands;
string toolsMenuName= "代码生成";
Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)BeginData.ApplicationObject.CommandBars)["MenuBar"];
CommandBarControl toolsControl;
try
{
toolsControl = menuBarCommandBar.Controls[toolsMenuName];
}
catch
{
CommandBar menuObj = (CommandBar)BeginData.ApplicationObject.Commands.AddCommandBar("代码生成(&C)",
vsCommandBarType.vsCommandBarTypeMenu,
((Microsoft.VisualStudio.CommandBars.CommandBars)BeginData.ApplicationObject.CommandBars)["MenuBar"], 4);
toolsControl = menuBarCommandBar.Controls[toolsMenuName];
}
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
DrawFactory.Factory.AddMenu(commands, toolsPopup);
}
}
改变 QueryStatus方法
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
{
if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
DrawFactory.Factory.RunCommand(commandName, ref status);
}
}
改边Exec方法
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
DrawFactory.Factory.RunMenu(commandName, ref handled);
}
}
注意 在QueryStatus方法里必须要执行
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
上一条:
下一条:
版权声明:
如果本站的资源使用了您的作品,请联系我们,我们会及时的注明
本站所有的资源均为免费自由下载,目的是让大家学习和交流
由于收集过程中几经转载,所以很多作品的原作者不详
如果您不愿在本站展示,请联系我们,我们会及时删除
相关评论
发表评论