C#VSTO-Powerpoint-TaskPanes位于不同的窗口中

问题描述 投票:0回答:2

我创建了一个带有自定义任务窗格的PowerPoint VSTO Addin - 以及一个功能区,其中切换按钮定义了自定义任务窗格的显示/隐藏状态。其基础是自定义任务窗格的Microsoft演练信息以及将功能区与“任务”窗格同步。因此,第一个PowerPoint窗口的一切工作正常。我可以在第二个和第三个PowerPoint窗口中显示“任务”窗格,但功能区上的切换按钮仅对最后打开/创建的PowerPoint窗口作出反应,而不响应活动PowerPoint窗口中显示/隐藏的“任务”窗格。

我找到了另一个解释完全相同问题的线程:C# VSTO-Powerpoint-TaskPanes in separate windows.

但我不明白答案我也不知道如何实现PowerPoint Inspector Wrapper。

我是C#的新手,只是得到像“Inspector Wrapper”这样的关键词对我来说更少。我已经花了几个小时搜索网但直到现在还不成功。

有没有机会获得PowerPoint的完整代码示例如何工作,必须做什么?

代码添加:我从一般演练中获取代码:https://msdn.microsoft.com/en-us/library/bb608590.aspx并使用事件更改它以进行新演示:

ThisAddIn.cs的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;

namespace PowerPointAddIn1
{
    public partial class ThisAddIn
    {
        private TaskPaneControl taskPaneControl1;
        private Microsoft.Office.Tools.CustomTaskPane taskPaneValue;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            this.Application.AfterNewPresentation += new Microsoft.Office.Interop.PowerPoint.EApplication_AfterNewPresentationEventHandler(NewPresentation);
            //taskPaneControl1 = new TaskPaneControl();
            //taskPaneValue = this.CustomTaskPanes.Add( taskPaneControl1, "MyCustomTaskPane");
            //taskPaneValue.VisibleChanged += new EventHandler(taskPaneValue_VisibleChanged);
        }

        void NewPresentation(Microsoft.Office.Interop.PowerPoint.Presentation oPres)
        {

            PowerPoint.Application app = this.Application;
            PowerPoint.DocumentWindow docWin = null;

            foreach (PowerPoint.DocumentWindow win in Globals.ThisAddIn.Application.Windows)
            {
                if (win.Presentation.Name == app.ActivePresentation.Name)
                {
                    docWin = win;
                }
            }

            this.taskPaneControl1 = new TaskPaneControl();
            this.taskPaneValue = this.CustomTaskPanes.Add(taskPaneControl1, "MyCustomTaskPane", docWin);
            this.taskPaneValue.VisibleChanged += new EventHandler(taskPaneValue_VisibleChanged);
        }

        private void taskPaneValue_VisibleChanged(object sender, System.EventArgs e)
        {
            Globals.Ribbons.ManageTaskPaneRibbon.toggleButton1.Checked =
            taskPaneValue.Visible;
        }

        public Microsoft.Office.Tools.CustomTaskPane TaskPane
        {
            get
            {
                return taskPaneValue;
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}
c# vsto powerpoint ribbon
2个回答
0
投票

我记得学习曲线哦。以下是我认为可以解决您的问题的示例。您需要将任务窗格链接到文档。我在这里依赖于新文档的命名方案,但是DocumentVariable将是一个更好的选择(它们在当前会话结束时被丢弃)。向演示文稿添加变量,将任务窗格ID存储在控件中,并比较它们以获取正确的任务窗格。

您需要一个XML功能区(可能使用功能区设计器,但这些功能不是很好)。我从中删除了一些样板和不相关的代码。

ThisAddIn.cs:

namespace PowerPointAddIn1
{
    public partial class ThisAddIn
    {
        public static int counter = 0;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            this.Application.AfterNewPresentation += Application_AfterNewPresentation;
        }

        private void Application_AfterNewPresentation(PowerPoint.Presentation Pres)
        {
            int count = ++counter;

            UserControl1 uc = new UserControl1("task pane " + count);
            CustomTaskPane ctp = CustomTaskPanes.Add(uc, "custom task pane " + count);
            ctp.Visible = true;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            return new Ribbon1();
        }
    }
}

Ribbon1.cs:

namespace PowerPointAddIn1
{
    [ComVisible(true)]
    public class Ribbon1 : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;

        public Ribbon1()
        {
        }

        public void toggleTaskPane(Office.IRibbonControl control, bool enabled)
        {
            var CTPs = Globals.ThisAddIn.CustomTaskPanes;
            var pres = Globals.ThisAddIn.Application.ActivePresentation;

            foreach (var x in CTPs)
            {
                if (pres.Name.EndsWith(x.Title.Replace("custom task pane ", "")))
                {
                    x.Visible = enabled;
                }
            }
        }

        public bool isPressed(Office.IRibbonControl control)
        {
            var CTPs = Globals.ThisAddIn.CustomTaskPanes;
            var pres = Globals.ThisAddIn.Application.ActivePresentation;
            foreach (var x in CTPs)
            {
                if (pres.Name.EndsWith(x.Title.Replace("custom task pane ", "")))
                {
                    return x.Visible;
                }
            }
            return false;
        }
    }
}

Ribbon1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <ribbon>
    <tabs>
      <tab idMso="TabAddIns">
        <group id="MyGroup"
               label="My Group">
          <checkBox id="mycheckbox" label="show task pane" onAction="toggleTaskPane" getPressed="isPressed" />
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

UsreControl1.cs(上面只有一个标签):

namespace PowerPointAddIn1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1(string labelValue)
        {
            InitializeComponent();

            label1.Text = labelValue;
        }
    }
}

0
投票

我只是想分享一些现在适合我的结果(感谢Chris给了我一些宝贵的意见)。我有一个customtaskpane管理,适用于每个演示文稿。唯一尚未实现的Thing是用户是否在单独的窗口(View / New Window)中打开文档。这一个我不知道如何管理。我可以测试它的价格现在可行。这是整个解决方案的链接:https://happypc-my.sharepoint.com/personal/roger_heckly_happy-pc_ch/_layouts/15/guestaccess.aspx?docid=0426d40dc5df74d66ba42a3b928111ce8&authkey=Aa6yX6QWUnqXp1jcUfGveL8

请注意 - 我是初学者 - 如果您有反馈/意见,请告诉我。当然,一些代码可以更容易编写等。

© www.soinside.com 2019 - 2024. All rights reserved.