• C#/Vsto中CustomTaskPanes和Ribbon的使用方法


      在工作中有一个需求,需要添加工作区选项卡,Excel中CustomTaskPanes面板很适合这样的场景,而非集中处理在Excel的Ribbon面板中,毕竟在大型项目中表现层已经过于复杂了。首先写一个显示Panes的方法。

    复制代码
    var sr = new OtherShouldReceiveUserControl(Wb, Wb.Application);
                                        var dpi = sr.dpiValue;
                                        sr.Name = "OtherShouldReceiveUserControl";
                                        var customTaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(sr, "快速导航");
                                        customTaskPane.DockPosition = MsoCTPDockPosition.msoCTPDockPositionFloating;
                                        customTaskPane.DockPositionRestrict = MsoCTPDockPositionRestrict
                                           .msoCTPDockPositionRestrictNoChange;
                                        customTaskPane.Visible = true;
    复制代码

    而这个方法我们会在worksheet active中触发。

    复制代码
    app = Globals.ThisAddIn.Application;
                    app.SheetActivate += App_SheetActivate;    
      private void App_SheetActivate(object Sh)
            {
                        CommonUtils.CallShowNavicatButton(CurWorkbook, curWorksheet.Name);
            }    
    复制代码

    在SheetActive中调用方法,不过有一个问题,每次Active处罚之后都会Add  OtherShouldReceiveUserControl 用户控件,它会出现用户控件重复添加的情况,所以你需要做一定的冗余处理。如何处理呢?

    复制代码
    Worksheet worksheet = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
                    var EnableNavicat = GetEnableNavicat();
                    if (EnableNavicat == "false")
                    {      return;   }
                    if (Wb.Application.Visible)
                    {
                        #region 删除导航,激活其他excel必须用目前方法
                        int i = -1;
                        int deleteIndex = -1;
                        foreach (var panel in Globals.ThisAddIn.CustomTaskPanes)
                        {
                            i++;
                            try
                            {
                                if (panel != null && (panel.Title == "快速导航")
                                {
                                    panel.Visible = false;
                                    deleteIndex = i;   break;
                                }
                            }
                            catch { }
                        }
                        if (deleteIndex >= 0)
                        {
                            //移除导航
                            try
                            {
                                Globals.ThisAddIn.CustomTaskPanes.RemoveAt(deleteIndex);
                            }
                            catch { }
                        }
    复制代码

    首先遍历 Globals.ThisAddIn.CustomTaskPanes 中的所有panel,如果Title是我们刚才添加的,或许你可以使用Tag来判断,这由你而定。随后通过 RemoveAt 方法来进行Delete操作。值得注意还有一个参数需要说说, MsoCTPDockPosition 改变Panel的 DockPosition 排列方式。一般使用Float即可。

    复制代码
    //
        // 摘要:
        //     Specifies the docking behavior of the custom task pane.
        public enum MsoCTPDockPosition
        {
            //
            // 摘要:
            //     Dock the task pane on the left side of the document window.
            msoCTPDockPositionLeft = 0,
            //
            // 摘要:
            //     Dock the task pane at the top of the document window.
            msoCTPDockPositionTop = 1,
            //
            // 摘要:
            //     Dock the task pane on the right side of the document window.
            msoCTPDockPositionRight = 2,
            //
            // 摘要:
            //     Dock the task pane at the bottom of the document window.
            msoCTPDockPositionBottom = 3,
            //
            // 摘要:
            //     Don't dock the task pane.
            msoCTPDockPositionFloating = 4
        }
    复制代码

      在此之前,我要创建一个Ribbon,百思不得其解的是Vsto是否只对应一个Ribbon面板,或者说是可以绑定多个,我多次试验后,发现是vsto项目确实对应一个Ribbon,当你创建了Vsto项目你会发现你的项目中还没有Ribbon,你需要手动创建。如图下。

     

      你以为这样你的项目中就生效了吗,你还需要将 Controlid 改为Custom,如果你想要第二个TabRibbon,你不需要在创建一个Ribbon,如果你创建了2个,那将都不显示,所有只能再创建一个Tab绑定一个 Controlid 。此时此刻,如图所示,已经达到了我们的效果。

    感谢您阅读本篇文章,祝您工作顺利。

     

  • 相关阅读:
    异硫氰酸荧光素FITC标记聚缩醛纳米粒FITC-Polyacetal;5-羧基荧光素标记Polyacetal聚缩醛纳米载体5-FAM-Polyacetal
    多个PDF怎么一起打印?教你两种打印方法
    【PHPWord】PHPWord导出PDF格式文件的几种方式以及最优解并附代码
    第64篇:史上最严重的APT供应链攻击事件,借助Solarwinds攻击欧美的流程图梳理和分析(上篇)...
    Golang中的GMP调度模型
    神经网络 深度神经网络,深度神经网络应用实例
    【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
    MySQL基础入门教程(InsCode AI 创作助手)
    RocketMQ运维工具
    代码工程化问题
  • 原文地址:https://www.cnblogs.com/ZaraNet/p/16023443.html