|
#define DIRECTLOAD
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Office.Core;
using Extensibility;
using EnvDTE;
using SHDocVw;
using mshtml;
namespace WebReports {
/// <summary>A class with static methods for building and managing commands in a host.</summary>
internal class CommandHelper {
/// <summary>
/// Create a new Command Bar and remove the existing Command Bar with the same name.
/// </summary>
/// <param name="applicationObject">The host application.</param>
/// <param name="szName">The name of the new command bar.</param>
/// <returns>The created CommandaBar object.</returns>
public static CommandBar AddCommandBar (_DTE applicationObject, String szName) {
Commands commands = applicationObject.Commands;
CommandBar cmdBar = null;
try {
CommandBar oldCmdBar = applicationObject.CommandBars[szName];
commands.RemoveCommandBar(oldCmdBar);
} catch(System.ArgumentException ex) {
ex = ex; // Thrown if command doesn't already exist.
} catch (Exception ex) {
System.Diagnostics.Debug.WriteLine(ex.Message, "WebReports.AddCommandBar");
} // try
try {
cmdBar = (CommandBar)commands.AddCommandBar(szName, vsCommandBarType.vsCommandBarTypeToolbar, null, 0);
} catch(System.ArgumentException ex) {
ex = ex; // Thrown if command doesn't already exist.
} catch (Exception ex) {
System.Diagnostics.Debug.WriteLine(ex.Message, "WebReports.AddCommandBar");
} // try
return(cmdBar);
} // AddCommandBar
/// <summary>Add one of the commands, dealing with the errors that might result.</summary>
/// <remarks>First version from MSDN Magazin 02/02</remarks>
/// <param name="applicationObject">The host application.</param>
/// <param name="addInInstance"></param>
/// <param name="szName">name of the command being added</param>
/// <param name="IconNr">Number of the corresponding icon in the resource dll.</param>
/// <param name="szButtonText">text displayed on menu or button</param>
/// <param name="szToolTip">text displayed in tool tip</param>
/// <param name="szKey">default key assignment, or empty string if none</param>
/// <param name="CmdBars">optional CommandBars where this command should be added.</param>
public static Command AddCommand (_DTE applicationObject, AddIn addInInstance,
String szName, int IconNr, String szButtonText, String szToolTip, String szKey, params CommandBar [] CmdBars) {
Command cmd = null;
object []GuidArray = new object[] { };
// The IDE identifies commands by their full name, which include the add-in name
try {
cmd = applicationObject.Commands.Item ("WebReports." + szName, -1);
cmd.Delete();
} catch(System.ArgumentException ex) {
ex = ex; // Thrown if command doesn't already exist.
} catch (Exception ex) {
System.Diagnostics.Debug.WriteLine(ex.Message, "WebReports.AddCommand");
} // try
try {
cmd = applicationObject.Commands.AddNamedCommand (addInInstance, szName, szButtonText, szToolTip,
false, IconNr, ref GuidArray,
(int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled);
} catch(System.ArgumentException ex) {
ex = ex; // Thrown if command already exist.
} catch (Exception ex) {
System.Diagnostics.Debug.WriteLine(ex.Message, "WebReports.AddCommand");
} // try
if ((szKey != null) && (szKey != "")) {
// a default keybinding specified
object [] bindings;
bindings = (object [])cmd.Bindings;
if (0 >= bindings.Length) {
// there is no preexisting key binding, so add the default
bindings = new object[1];
bindings[0] = (object)szKey;
cmd.Bindings = (object)bindings;
} // if
} // if
// Append Control at end of CommandBar
foreach (CommandBar c in CmdBars) {
if (c != null)
cmd.AddControl(c, c.Controls.Count+1);
} // foreach
return(cmd);
} // AddCommand
/// <summary>
/// Remove the registration for a command.
/// </summary>
/// <param name="applicationObject">The host application.</param>
/// <param name="commandName"></param>
public static void RemoveCommand(_DTE applicationObject, string commandName) {
try {
Command cmd = applicationObject.Commands.Item(commandName, -1);
if (cmd != null) cmd.Delete();
} catch (Exception ex) {
System.Diagnostics.Debug.WriteLine(ex.Message, "WebReports.RemoveCommand");
} // try
} // RemoveCommand
/// <summary>
/// Remove a Control from a CommnandBar
/// </summary>
/// <param name="applicationObject">The host application.</param>
/// <param name="commandBarName"></param>
/// <param name="controlName"></param>
public static void RemoveCommandControl(_DTE applicationObject, string commandBarName, string controlName) {
// Remove the controls from the CommandBars
try {
Microsoft.Office.Core.CommandBar bar = applicationObject.CommandBars[commandBarName];
CommandBarControl ctrl = null;
for (int n = 1; n <= bar.Controls.Count; n++) {
string cap = bar.Controls[n].Caption ;
if (bar.Controls[n].Caption == controlName)
ctrl = bar.Controls[n];
} // for
// CommandBarControl ctrl = bar.Controls[controlName];
if (ctrl != null) ctrl.Delete(false);
} catch (Exception ex) {
System.Diagnostics.Debug.WriteLine(ex.Message, "WebReports.RemoveCommandControl");
} // try
} // RemoveCommandControl
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here