VSTO(文档级):在Excel中个人上下文菜单(右键菜单)上下文、右键菜单、菜单、文档

2023-09-03 16:41:27 作者:獨自去偷歡゜

是否有可能为右键菜单通过C#(VSTO)禁用或配置(单元格,范围)。 如果是的话,我怎么能实现它(在文档级VSTO Excel应用程序)

Is it possible to disable or configure the (cell, range) context menu via C# (VSTO). If yes, how can I implement it (in a document-level VSTO Excel application)

例如我想禁用上下文菜单中的某些项目(如复制/粘贴),并添加新项目或一个完整的自己的菜单替换标准的上下文菜单!

For example I want to disable some items in the context menu (e.g. copy/paste) and add new items or replace the standard context menu with a complete own menu!

是智能标记一个很好的替代方面menues在Excel?

Are Smarttags a good alternative to context menues in Excel?

推荐答案

在下面是一些粗糙的样品code有意见 这是在 VS2010创建和对测试 Excel 2010中 第一步是创建一个新的 Excel 2010中加载项目 然后在下面添加到默认的code样品code内部产生 ThisAddin.cs 这code将增加一个新的菜单项,并删除剪切/复制/粘贴菜单项时,在包含'ABC'单细胞右键点击。这是模拟变化的基础上一单元格的内容的上下文菜单。

Here's some rough sample code with comments This was created in VS2010 and tested against Excel 2010 First step was to created a new Excel 2010 Add-In project Then added the sample code below to the default code generated inside ThisAddin.cs. This code will add a new menu item and remove the cut/copy/paste menu items when right-clicking on a single cell containing 'abc'. This is to simulate changing the context menu based on the contents of a cell.




    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools.Excel;
    using System.Diagnostics;
    using Microsoft.Office.Interop.Excel;

    namespace Excel_Menu
    {
        public partial class ThisAddIn
        {
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                ResetCellMenu();  // reset the cell context menu back to the default

                // Call this function is the user right clicks on a cell
                this.Application.SheetBeforeRightClick+=new Excel.AppEvents_SheetBeforeRightClickEventHandler(Application_SheetBeforeRightClick);
            }

            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }

            private Office.CommandBar GetCellContextMenu()
            {
                return this.Application.CommandBars["Cell"];
            }

            private void ResetCellMenu()
            {
                GetCellContextMenu().Reset(); // reset the cell context menu back to the default
            }

            private void Application_SheetBeforeRightClick(object Sh, Range Target, ref bool Cancel)
            {
                ResetCellMenu();  // reset the cell context menu back to the default

                if (Target.Cells.Count == 1)   // sample code: if only a single cell is selected
                {
                    if (Target.Cells[1, 1].Value == "abc") // sample code: if the signle cell contains 'abc'
                    {
                        AddExampleMenuItem();
                        RemoveCutCopyPasteMenuItems();
                    }
                }
            }

            private void AddExampleMenuItem()
            {
                Office.MsoControlType menuItem = Office.MsoControlType.msoControlButton;
                Office.CommandBarButton exampleMenuItem = (Office.CommandBarButton)GetCellContextMenu().Controls.Add(menuItem, missing, missing, 1, true);

                exampleMenuItem.Style = Office.MsoButtonStyle.msoButtonCaption;
                exampleMenuItem.Caption = "Example Menu Item";
                exampleMenuItem.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(exampleMenuItemClick);
            }

            private void RemoveCutCopyPasteMenuItems()
            {
                Office.CommandBar contextMenu = GetCellContextMenu();

                for (int i = contextMenu.Controls.Count; i > 0; i--)
                {
                    Office.CommandBarControl control = contextMenu.Controls[i];

                    if (control.Caption == "Cu&t") control.Delete();  // Sample code: remove cut menu item
                    else if (control.Caption == "&Copy") control.Delete();  // Sample code: remove copy menu item
                    else if (control.accDescription.Contains("Paste")) control.Delete(); // Sample code: remove any paste menu items
                }
            }

            void exampleMenuItemClick(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
            {
                System.Windows.Forms.MessageBox.Show("Example Menu Item clicked");
            }

            #region VSTO generated code

            /// 
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// 
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }

            #endregion
        }
    }