ZaraNet

  在工作中有一个需求,需要添加工作区选项卡,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 。此时此刻,如图所示,已经达到了我们的效果。

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

 

分类:

Vsto

技术点:

相关文章: