【问题标题】:VSTO (Document-level): Individual context menu in Excel (right click menu)VSTO(文档级):Excel 中的单个上下文菜单(右键菜单)
【发布时间】:2013-06-21 23:43:13
【问题描述】:

是否可以通过 C# (VSTO) 禁用或配置(单元格、范围)上下文菜单。 如果是,我该如何实现它(在文档级 VSTO Excel 应用程序中)

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

智能标签是 Excel 中上下文菜单的良好替代品吗?

【问题讨论】:

  • 是的,可以使用 Globals.ThisWorkbook.Application.Commandbars。我有一段时间没有这样做了,但这应该可以让你开始。

标签: c# .net excel vsto office-interop


【解决方案1】:
  • 这是一些带有 cmets 的粗略示例代码
  • 这是在 VS2010 中创建的,并针对 Excel 2010 进行了测试
  • 第一步是创建一个新的Excel 2010 Add-In项目
  • 然后将下面的示例代码添加到 ThisAddin.cs 内部生成的默认代码中。
  • 当右键单击包含“abc”的单个单元格时,此代码将添加一个新菜单项并删除剪切/复制/粘贴菜单项。这是为了模拟根据单元格的内容更改上下文菜单。
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 } }

【讨论】:

  • 谢谢。但这是加载项/应用程序级 VSTO 项目的解决方案。我需要一个文档级应用程序的解决方案!!
  • 我之前没有创建过文档级加载项,但我刚刚创建了一个新的 Excel 2010 工作簿应用程序。这与您的使用方式相符吗?我能够在 ThisWorkbook.cs 中使用上面的代码,并且效果相同。
猜你喜欢
  • 1970-01-01
  • 2011-09-30
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
  • 2017-04-22
  • 1970-01-01
相关资源
最近更新 更多