查看: 77|回覆: 0

C# 管理多个工作簿的任务窗格

[複製鏈接]

3

主題

0

回帖

0

積分

热心网友

金币
0
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2008-9-29
發表於 2025-3-30 08:31:00 | 顯示全部樓層 |閲讀模式

CustomTaskPanes.Add方法,它自动把任务窗格添加到当前的工作簿上去了。怎样才能将工作簿对象和任务窗格关联起来?
方法是:
ThisAddIn.cs中创建一个字典管理任务窗格

private Dictionary<string, CustomTaskPane> _createdPanes = new Dictionary<string, CustomTaskPane>();

    /// <summary>
    /// 按名称获取任务窗格(如果当前excel窗口存在,则返回现有实例,否则使用taskPaneCreatorFunc创建一个)。
    /// </summary>
    /// <param name="taskPaneId">一个唯一字符串来标识任务窗格</param>
    /// <param name="taskPaneTitle">任务窗格的标题</param>
    /// <param name="taskPaneWidth">任务窗格的宽度</param>
    /// <param name="taskPaneDockPosition">任务窗格的停靠位置</param>
    /// <param name="taskPaneCreatorFunc">构造任务窗格的函数。</param>
    public CustomTaskPane GetTaskPane(
               string taskPaneId,
               string taskPaneTitle,
               int taskPaneWidth,
               Office.MsoCTPDockPosition taskPaneDockPosition,
               Func<UserControl> taskPaneCreatorFunc
           )
    {
        //string key = string.Format("{0}({1})", taskPaneId, Globals.ThisAddIn.Application.Hwnd);
        string key = $"{taskPaneId}({Globals.ThisAddIn.Application.Hwnd})";

        if (!_createdPanes.ContainsKey(key))
        {
            // 忽略第三个参数,默认在当前窗口新建一个任务窗格
            var taskPane = Globals.ThisAddIn.CustomTaskPanes.Add(taskPaneCreatorFunc(), taskPaneTitle);
            taskPane.Width = taskPaneWidth;
            taskPane.DockPosition = taskPaneDockPosition;
            _createdPanes[key] = taskPane;
        }
        return _createdPanes[key];
    }

在Ribbon1.cs中设置一个按钮 调用 并显示或隐藏
private void button1_Click(object sender, RibbonControlEventArgs e)
{
//Globals.ThisAddIn.myPane.Visible = ! Globals.ThisAddIn.myPane.Visible;
{
//调用自定义任务窗格方法来获取或创建任务窗格
var taskpane = Globals.ThisAddIn.GetTaskPane(
"A",
"设置",
200,
Office.MsoCTPDockPosition.msoCTPDockPositionLeft,
() => new UserControl1()
);
taskpane.Visible = !taskpane.Visible;
}

    }


来源:https://www.cnblogs.com/WxxYdl/p/18800279
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部