簡體   English   中英

在單獨的窗口中使用C#VSTO-Powerpoint-TaskPanes

[英]C# VSTO-Powerpoint-TaskPanes in separate windows

我已經創建了一個帶有自定義任務窗格的PowerPoint VSTO加載項-一個功能區,其中一個切換按鈕定義了自定義任務窗格的顯示/隱藏狀態。 其基礎是用於自定義任務窗格以及將功能區與任務窗格同步的Microsoft演練信息。 因此,票價在第一個PowerPoint窗口中一切正常。 我能夠在第二個和第三個PowerPoint窗口中顯示“任務”窗格,但是功能區上的切換按鈕僅對最后打開/創建的PowerPoint窗口做出反應,而對活動的PowerPoint窗口中顯示/隱藏的“任務”窗格不起作用。

我在這里找到了另一個解釋完全相同的問題的線程: 在單獨的窗口中使用C#VSTO-Powerpoint-TaskPanes。

但是我也不明白答案,我也不知道如何實現PowerPoint Inspector包裝器。

我是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
    }
}

我記得學習曲線很好。 我相信這是一個示例,可以解決您的問題。 您需要將任務窗格鏈接到文檔。 我在這里依賴於新文檔的命名方案,但是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;
        }
    }
}

我只想分享我現在可以使用的結果(感謝克里斯給了我一些寶貴的意見)。 我確實有一個customtaskpane管理,適用於每個演示文稿。 唯一尚未實現的功能是用戶是否在單獨的窗口(查看/新窗口)中打開文檔。 我不知道該如何管理。 盡我所能測試,現在可以使用了。 這是整個解決方案的鏈接: https : //happypc-my.sharepoint.com/personal/roger_heckly_happy-pc_ch/_layouts/15/guestaccess.aspx?docid=0426d40dc5df74d66ba42a3b928111ce8&authkey=Aa6yX6QWUnqXp1jcUfGveL8

請注意-我是新手-如果您有反饋意見/建議,請告訴我。 可以肯定的是,某些代碼可能更容易編寫等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM