Example #1
0
        private void RemoveRightClickMenu()
        {
            // 这里我写了一个循环,目标是清理所有由我创建的右键按钮,尤其是由于Addin Crash时所遗留的按钮
            CommandBarControl control = textCommandBar.FindControl(MsoControlType.msoControlButton, missing, "BookMarkAddin", true, true);

            while (control != null)
            {
                control.Delete(true);
                control = textCommandBar.FindControl(MsoControlType.msoControlButton, missing, "BookMarkAddin", true, true);
            }
        }
        private void InstallButton(CommandBar cb, string id)
        {
            CommandBarButton thisButton;

            _buttons.TryGetValue(id, out thisButton);

            // Recreate the button, otherwise it's state may be broken in Visio in some cases
            if (thisButton != null)
            {
                thisButton.Click -= CommandBarButtonClicked;
                _buttons.Remove(id);
                Marshal.ReleaseComObject(thisButton);
            }

            var button = (CommandBarButton)cb.FindControl(Tag: id) ??
                         (CommandBarButton)cb.Controls.Add(MsoControlType.msoControlButton);

            button.Enabled = Globals.ThisAddIn.IsCommandEnabled(id);

            var checkState = Globals.ThisAddIn.IsCommandChecked(id);

            button.State = checkState ? MsoButtonState.msoButtonDown : MsoButtonState.msoButtonUp;

            button.Tag     = id;
            button.Caption = Globals.ThisAddIn.GetCommandLabel(id);
            SetCommandBarButtonImage(button, id);

            button.Click += CommandBarButtonClicked;

            _buttons.Add(id, button);
        }
Example #3
0
        /// <summary>
        /// Adds the command bar.
        /// </summary>
        /// <param name="cmdBr">The CMD br.</param>
        /// <param name="handler">The handler.</param>
        /// <param name="index">The index.</param>
        /// <param name="tag">The tag.</param>
        /// <param name="caption">The caption.</param>
        /// <returns></returns>
        private CommandBarButton AddCommandBar(CommandBar cmdBr, _CommandBarButtonEvents_ClickEventHandler handler, int index, string tag, string caption)
        {
            CommandBarButton cmdBtn = (CommandBarButton)cmdBr.FindControl(MsoControlType.msoControlButton, 0, tag, missing, missing);

            if ((cmdBtn != null))
            {
                cmdBtn.Delete(true);
            }

            cmdBtn         = (CommandBarButton)cmdBr.Controls.Add(MsoControlType.msoControlButton, missing, missing, index, true);
            cmdBtn.Style   = MsoButtonStyle.msoButtonCaption;
            cmdBtn.Caption = caption;
            cmdBtn.Tag     = tag;
            cmdBtn.Visible = true;

            cmdBtn.Click -= handler;
            cmdBtn.Click += handler;

            if (!commandBarsTags.Contains(tag))
            {
                commandBarsTags.Add(tag);
            }

            return(cmdBtn);
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cbar"> commandBar on which the button is added.</param>
        /// <param name="tag"> button tag</param>
        /// <param name="desc">button description</param>
        /// <param name="caption">button caption</param>
        /// <param name="before"></param>
        /// <param name="temp"></param>
        /// <param name="handler"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static CommandBarButton addButton(CommandBar cbar, String tag, String desc,
                                                 String caption, Object before, bool temp,
                                                 _CommandBarButtonEvents_ClickEventHandler handler, String parameter)
        {
            CommandBarButton foundButton = (CommandBarButton)cbar.FindControl(
                MsoControlType.msoControlButton,
                Type.Missing, tag, false, true);

            if (foundButton != null)
            {
                foundButton.Caption = caption;
                setClickHandler(foundButton, handler);
                foundButton.Visible   = true;
                foundButton.Parameter = parameter;
                return(foundButton);
            }

            CommandBarButton button = (CommandBarButton)cbar.Controls.Add(
                MsoControlType.msoControlButton,
                Type.Missing, Type.Missing, before, temp);

            setClickHandler(button, handler);

            button.Tag             = tag;
            button.DescriptionText = desc;
            button.BeginGroup      = false;
            button.Caption         = caption;
            button.Style           = MsoButtonStyle.msoButtonIconAndCaption;
            button.Visible         = true;
            button.Parameter       = parameter;

            return(button);
        }
        public object FindControl(object Type, object Id, object Tag, object Visible, object Recursive)
        {
            var o = _bar.FindControl(Type, Id, Tag, Visible, Recursive);

            if (null == o)
            {
                return(null);
            }

            return(ShellObjectFactory.CreateFromCommandBarControl(o));
        }
Example #6
0
        /// <summary>
        ///     Implements the OnDisconnection method of the IDTExtensibility2 interface.
        ///     Receives notification that the Add-in is being unloaded.
        /// </summary>
        /// <param term='disconnectMode'>
        ///      Describes how the Add-in is being unloaded.
        /// </param>
        /// <param term='custom'>
        ///      Array of parameters that are host application specific.
        /// </param>
        /// <seealso class='IDTExtensibility2' />
        public void OnDisconnection(
            Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
        {
            if (disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
            {
                Debug.WriteLine("OnDisconnection");

                if (button != null)
                {
                    try
                    {
                        // We must unhook the event delegate, otherwise we'd end up
                        // with multiple invocations if the user disconnects/reconnects
                        // the add-in.
                        button.Click -=
                            new _CommandBarButtonEvents_ClickEventHandler(button_Click);

                        // We must delete the button when the user disconnects the add-in
                        // during a session. We don't need to do this when the host shuts
                        // down because we create the button as temporary (so it gets
                        // deleted on shutdown anyway).

                        // Deleting the button works fine for most Office apps.
                        // However, there's a race condition in Word.
                        // Unhooking the event delegate always works, but _any_
                        // subsequent access to the button sometimes throws 0x800A01A8,
                        // regardless of whether or not a shim is used. The workaround
                        // is to get the button again so that we can delete it.

                        if (hostName == "Microsoft Word")
                        {
                            object missing = Type.Missing;
                            button = (CommandBarButton)
                                     bar.FindControl(MsoControlType.msoControlButton,
                                                     missing, buttonName, true, true);
                        }

                        // We do this for all Office hosts.
                        button.Delete(false);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
            else
            {
                Debug.WriteLine("OnDisconnection - ext_dm_HostShutdown");
            }
            applicationObject = null;
        }
Example #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="theBar"></param>
        /// <param name="tag"></param>
        public static void RemoveButton(CommandBar theBar, String tag)
        {
            try
            {
                CommandBarButton foundButton = (CommandBarButton)
                                               theBar.FindControl(MsoControlType.msoControlButton,
                                                                  Type.Missing, tag, true, true);
                if (foundButton != null)
                {
                    foundButton.Delete(false);
                    foundButton = null;
                }
            }

            catch (System.Exception ex)
            {
                Logger.WriteEntry(LogLevel.Warning, ex.GetType() + ":" + ex.Message);
            }
        }
 public CommandBarPopup FindPopupMenu(string tag)
 {
     return((CommandBarPopup)ActiveMenuBar.FindControl(MsoControlType.msoControlPopup, _missing, tag, true, true));
 }
Example #9
0
        private CommandBarControl GetControl(CommandBar bar, string tag)
        {
            Object missing = Missing.Value;

            return(bar.FindControl(missing, missing, tag, missing, missing));
        }
Example #10
0
        /// <summary>
        ///      Implements the OnStartupComplete method of the IDTExtensibility2 interface.
        ///      Receives notification that the host application has completed loading.
        /// </summary>
        /// <param term='custom'>
        ///      Array of parameters that are host application specific.
        /// </param>
        /// <seealso class='IDTExtensibility2' />
        public void OnStartupComplete(ref System.Array custom)
        {
            Debug.WriteLine("OnStartupComplete");

            try
            {
                // We want to get the Tools menu, but first we must
                // find the name of the host application, because the
                // way we get to the Tools menu is different depending
                // on which host application we're running in.
                Type        applicationType = applicationObject.GetType();
                CommandBars bars            = null;
                hostName = (string)applicationType.InvokeMember(
                    "Name", System.Reflection.BindingFlags.GetProperty,
                    null, applicationObject, null);

                if (hostName == "Outlook")
                {
                    // For Outlook, we get to the CommandBars via the ActiveExplorer.
                    object explorer = applicationType.InvokeMember(
                        "ActiveExplorer", System.Reflection.BindingFlags.GetProperty,
                        null, applicationObject, null);
                    if (explorer != null)
                    {
                        Type explorerType = explorer.GetType();
                        bars = (CommandBars)explorerType.InvokeMember(
                            "CommandBars", System.Reflection.BindingFlags.GetProperty,
                            null, explorer, null);
                    }
                }
                else if (hostName == "Microsoft Office InfoPath")
                {
                    // For InfoPath, we get to the CommandBars via the ActiveWindow.
                    object window = applicationType.InvokeMember(
                        "ActiveWindow", System.Reflection.BindingFlags.GetProperty,
                        null, applicationObject, null);
                    if (window != null)
                    {
                        Type windowType = window.GetType();
                        bars = (CommandBars)windowType.InvokeMember(
                            "CommandBars", System.Reflection.BindingFlags.GetProperty,
                            null, window, null);
                    }
                }
                else
                {
                    // For all other Office apps, we get to the CommandBars directly
                    // from the application object.
                    bars = (CommandBars)applicationType.InvokeMember(
                        "CommandBars", System.Reflection.BindingFlags.GetProperty,
                        null, applicationObject, null);
                }
                bar = bars["Tools"];

                // Add our custom button to the bar, if it's not already there.
                object missing = Type.Missing;
                button = (CommandBarButton)
                         bar.FindControl(MsoControlType.msoControlButton,
                                         missing, buttonName, true, true);
                if (button == null)
                {
                    // It's not there, so add our button to the commandbar.
                    button = (CommandBarButton)bar.Controls.Add(
                        MsoControlType.msoControlButton, 1, missing, missing, true);
                    button.FaceId  = 59;
                    button.Caption = buttonName;
                    button.Tag     = buttonName;
                    button.Style   = MsoButtonStyle.msoButtonIconAndCaption;
                }

                button.Click +=
                    new _CommandBarButtonEvents_ClickEventHandler(button_Click);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }