插件片段临时代码
2015-04-15 15:20:10 访问(1361) 赞(0) 踩(0)
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; string toolsMenuName;
try { //若要将此命令移动到另一个菜单,则将“工具”一词更改为此菜单的英文版。 // 此代码将获取区域性,将其追加到菜单名中,然后将此命令添加到该菜单中。 // 您会在此文件中看到全部顶级菜单的列表 // CommandBar.resx. string resourceName; ResourceManager resourceManager = new ResourceManager("PropertyBuild.CommandBar", Assembly.GetExecutingAssembly()); CultureInfo cultureInfo = new CultureInfo(_applicationObject.LocaleID);
if(cultureInfo.TwoLetterISOLanguageName == "zh") { System.Globalization.CultureInfo parentCultureInfo = cultureInfo.Parent; resourceName = String.Concat(parentCultureInfo.Name, "Tools"); } else { resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools"); } toolsMenuName = resourceManager.GetString(resourceName); } catch { //我们试图查找“工具”一词的本地化版本,但未能找到。 // 默认值为 en-US 单词,该值可能适用于当前区域性。 toolsMenuName = "Tools"; }
//将此命令置于“工具”菜单上。 //查找 MenuBar 命令栏,该命令栏是容纳所有主菜单项的顶级命令栏: Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"]; string menuName = "Code Window"; //在 MenuBar 命令栏上查找“工具”命令栏: CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName]; CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
CommandBar contextCommandBar = ((CommandBars)_applicationObject.CommandBars)[menuName]; //如果希望添加多个由您的外接程序处理的命令,可以重复此 try/catch 块, // 只需确保更新 QueryStatus/Exec 方法,使其包含新的命令名。 try { //将一个命令添加到 Commands 集合: Command command = commands.AddNamedCommand2(_addInInstance, "PropertyBuild", "PropertyBuild", "Executes the command for PropertyBuild", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton); //将对应于该命令的控件添加到“工具”菜单: if ((command != null) && (toolsPopup != null) && contextCommandBar!=null) {
command.AddControl(contextCommandBar, 1); command.AddControl(toolsPopup.CommandBar, 1); } } catch(System.ArgumentException) { //如果出现此异常,原因很可能是由于具有该名称的命令 // 已存在。如果确实如此,则无需重新创建此命令,并且 // 可以放心忽略此异常。 } } }
|
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理 |
| #7 得分:0回复于: 2012-08-26 20:31:31 public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) { handled = false; if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault) { if(commandName == "PropertyBuild.Connect.PropertyBuild") { handled = true; CreateNewFileWithCopyrightInfo(); return; } } } private DTE2 applicationObject;
public void CreateNewFileWithCopyrightInfo() {
try { // Create a copyright text for each code item in the project // Get the array of projects from the active solution System.Array theProjects = (System.Array)applicationObject.ActiveSolutionProjects; EnvDTE.Project theProject = null; // make sure there is an active solution containing projects if (theProjects.Length > 0) { // This add-in only works for the first project in the solution, // This can be extended for multiple projects by putting a for loop here like // foreach (Project theProject in theProjects) theProject = (EnvDTE.Project)(theProjects.GetValue(0)); // Go through each project item in the project foreach (ProjectItem p in theProject.ProjectItems) { Trace.WriteLine("ProjectItem = " + p.Name); // Check to see if its a c# file if (p.Name.Substring(p.Name.Length - 3, 3) == ".cs") { // Open the file as a source code file EnvDTE.Window theWindow = p.Open(Constants.vsViewKindCode); //Get a handle to the new document in the open window TextDocument objTextDoc = (TextDocument)theWindow.Document.Object("TextDocument"); EditPoint objEditPoint = (EditPoint)objTextDoc.StartPoint.CreateEditPoint(); //Create an EditPoint and add some text. objEditPoint.Insert("// This Code was created by Microgold Software Inc. for educational purposes" + @" " + "// Copyright Microgold Software Inc. " + DateTime.Now.ToLongDateString() + " "); // Get the filename from the project item and save the file with the copyright information string fName = p.get_FileNames(0); p.SaveAs(fName); } // end if ".cs" file } // end for each project item } // end if any projects exists in projects collection } // end try block catch (Exception e) { Trace.WriteLine(e.Message.ToString()); }
} private AddIn _addInInstance; } |
|
if(poWatchContextMenu == null)
{
_CommandBars commandBars = Application.CommandBars;
CommandBar vsBarProject = Application.CommandBars["Code Window"];
poWatchContextMenu = vsBarProject.Controls.Add(MsoControlType.msoControlButton, 1, "", 1, true);
poWatchContextMenu.Caption = "My Watch";
poWatchContextMenu.TooltipText = "";
CommandBarEvents menuItemHandler = (CommandBarEvents)Application.DTE.Events.get_CommandBarEvents(poWatchContextMenu);
menuItemHandler.Click += new _dispCommandBarControlEvents_ClickEventHandler(Watch_Click);
}
上一条:
下一条:
版权声明:
如果本站的资源使用了您的作品,请联系我们,我们会及时的注明
本站所有的资源均为免费自由下载,目的是让大家学习和交流
由于收集过程中几经转载,所以很多作品的原作者不详
如果您不愿在本站展示,请联系我们,我们会及时删除
相关评论
发表评论