VSTO 禁止EXCEL工作表中的右击菜单
private void ThisWorkbook_Startup(object sender, System.EventArgs e) { this.ActivateEvent += new Microsoft.Office.Interop.Excel.WorkbookEvents_ActivateEventHandler(ThisWorkbook_ActivateEvent); this.SheetActivate += new Microsoft.Office.Interop.Excel.WorkbookEvents_SheetActivateEventHandler(ThisWorkbook_SheetActivate); } void ThisWorkbook_ActivateEvent() { Excel.Worksheet sh = ActiveSheet as Excel.Worksheet; sh.BeforeRightClick += new Microsoft.Office.Interop.Excel.DocEvents_BeforeRightClickEventHandler(sh_BeforeRightClick); } void ThisWorkbook_SheetActivate(object Sh) { Excel.Worksheet sh = ActiveSheet as Excel.Worksheet; sh.BeforeRightClick += new Microsoft.Office.Interop.Excel.DocEvents_BeforeRightClickEventHandler(sh_BeforeRightClick); } void sh_BeforeRightClick(Microsoft.Office.Interop.Excel.Range Target, ref bool Cancel) { Cancel = true; } private void ThisWorkbook_Shutdown(object sender, System.EventArgs e) { }
可能有人会问,除了ThisWorkbook_SheetActivate事件,为什么还要加上一个ThisWorkbook_ActivateEvent事件?
这是因为,打开表的时候,当前表并不会激发ThisWorkbook_SheetActivate事件,所以还需要加上工作簿的激活事件。
作者:laoyebin(Paladin.lao)
出处:http://www.cnblogs.com/laoyebin/
网赚 :https://laoyebin.com/
优惠码发布:https://yhmfb.com/
导读网:https://daodu.co/
外汇吧:https://waihui.pub/
本文版权归作者和博客园共有,欢迎转载,但请保留作者信息和原文链接,非常感谢。
反馈文章质量,你可以通过快速通道评论: