其他分享
首页 > 其他分享> > VSTO Office二次开发对PPT自定义任务窗格测试

VSTO Office二次开发对PPT自定义任务窗格测试

作者:互联网

上篇文章对VSTO Office二次开发操作PPT功能做了简单测试,主要是如何创建一个外接程序并在新建PPT幻灯片时添加自定义文本信息、如何简单自定义任务窗格、如何添加可视化功能区的简单介绍,应该对于VSTO如何操作PPT有简单了解,本篇分享对于自定义任务窗格的相关测试。

一、功能区按钮控制CustomTaskPane的显示隐藏
1.新建一个外接程序,添加可视化功能区和用户自定义控件,在可视化功能区添加两个按钮,一个用来控制自定义任务窗格显示,一个用来控制自定义任务窗格隐藏的。
2.先将自定义任务窗格关联到外接程序,在ThisAddIn.cs文件添加如下代码:

public CustomTaskPane myCustomTaskPanel = null;//自定义
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            AddTaskPane();//添加自定义Task Pane
        }

     /// <summary>
        /// 添加自定义的Task Pane
        /// </summary>
        private void AddTaskPane()
        {
            UCTaskPane taskPane = new UCTaskPane();//自定义用户控件名称
            myCustomTaskPanel = this.CustomTaskPanes.Add(taskPane, "My Task Pane");
            myCustomTaskPanel.Width = 200;
            myCustomTaskPanel.Visible = true;
        }

  3.在可视化功能区的两个按钮添加如下代码:

/// <summary>
        /// 打开自定义任务窗格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenTask_Click(object sender, RibbonControlEventArgs e)
        {
            if (Globals.ThisAddIn.myCustomTaskPanel != null)
            {
                Globals.ThisAddIn.myCustomTaskPanel.Visible = true;
            }
        }
        /// <summary>
        /// 关闭自定义任务窗格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClosePanel_Click(object sender, RibbonControlEventArgs e)
        {
            if (Globals.ThisAddIn.myCustomTaskPanel != null)
            {
                Globals.ThisAddIn.myCustomTaskPanel.Visible = false;
            }
        }

  

F5运行,看看效果。也可以在可视化功能区只需添加一个切换按钮(ToggleButton),在按钮点击事件添加一行代码也能很好的控制任务窗格的显示与隐藏:
Globals.ThisAddIn.myCustomTaskPanel.Visible = toggleBtn.Checked;
主要是如何去调用自定义任务窗格:Globals.ThisAddIn.myCustomTaskPanel,然后就是对其进行控制操作(自定义任务窗格可以很好的实现类似WPS的素材库呈现的功能)。

二、自定义任务窗格对幻灯片的相关操作
功能:在自定义任务窗格中,添加一个日期控件,通过其DateChanged事件实现对PPT中对应幻灯片的相关操作,添加文字、添加图片、添加多媒体对象
1.新添加一个用户自定义控件,添加一个日期控件,在其DateChanged事件添加如下代码:

private PowerPoint.Shape textbox;
 private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            if (textbox != null)
            {
                textbox.Delete();
            }
            PowerPoint.Slides slides = Globals.ThisAddIn.Application.ActivePresentation.Slides;//获取当前应用程序的所有PPT文档
            for (int i = 1; i <= slides.Count; i++)//遍历该文档集合,添加文本框
            {
                 PowerPoint.Slide slide = slides[i];
                 textbox = slide.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, 50, 100, 600, 50);//向当前PPT添加文本框
               textbox.TextFrame.TextRange.Text = textContent;//设置文本框的内容
                textbox.TextFrame.TextRange.Font.Size = 48;//设置文本字体大小
               textbox.TextFrame.TextRange.Font.Color.RGB = Color.DarkViolet.ToArgb();//设置文本颜色
            }
        }

  2.F5运行,点击日期就会在PPT中每一个的幻灯片中将添加一个TextBox对象,位置在(左50,顶100),大小(宽600,高50)。
主要是如何去获取当前演示文稿的所有幻灯片:Globals.ThisAddIn.Application.ActivePresentation.Slides
3.简单封装扩展:添加文字、添加图片、添加多媒体对象、OLE对象

/// <summary>
        /// 添加TextBox文本框
        /// </summary>
        /// <param name="slide">要添加文本框的幻灯片</param>
        /// <param name="textContent">文本框显示内容</param>
        private void AddTextBox(PowerPoint.Slide slide, string textContent)
        {
            PowerPoint.Shape textbox;
            textbox = slide.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, 50, 100, 600, 50);//向当前PPT添加文本框
            textbox.TextFrame.TextRange.Text = textContent;//设置文本框的内容
            textbox.TextFrame.TextRange.Font.Size = 48;//设置文本字体大小
            textbox.TextFrame.TextRange.Font.Color.RGB = Color.DarkViolet.ToArgb();//设置文本颜色
        }

        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="slide"></param>
        /// <param name="shape"></param>
        /// <param name="filePath"></param>
        private void AddADPicture(PowerPoint.Slide slide, PowerPoint.Shape shape, string filePath)
        {
            PowerPoint.Shape pic;
            pic = slide.Shapes.AddPicture(filePath, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, shape.Left, shape.Top, shape.Width, shape.Height);
            pic.Name = "AD" + shape.Name;//
        }

        /// <summary>
        /// 添加音频\视频文件
        /// </summary>
        /// <param name="slide"></param>
        /// <param name="shape"></param>
        /// <param name="filePath"></param>
        private void AddMediaObj(PowerPoint.Slide slide, PowerPoint.Shape shape, string filePath)
        {
            PowerPoint.Shape media;
            media = slide.Shapes.AddMediaObject(filePath, shape.Left, shape.Top, shape.Width, shape.Height);
            media.Name = shape.Name;
        }

        /// <summary>
        /// 通过文件路径添加OLE对象
        /// </summary>
        /// <param name="slide"></param>
        /// <param name="shape"></param>
        /// <param name="className"></param>
        private void AddClassOLEObj(PowerPoint.Slide slide, PowerPoint.Shape shape,string className)
        {
            PowerPoint.Shape oleObj;
            oleObj=slide.Shapes.AddOLEObject(Left:shape.Left,Top:shape.Top,Width:shape.Width,Height:shape.Height,ClassName:className);
            oleObj.Name = shape.Name;
        }

        /// <summary>
        /// 通过ClassName添加OLE对象
        /// </summary>
        /// <param name="slide"></param>
        /// <param name="shape"></param>
        /// <param name="fileName"></param>
        private void AddFileOLEObj(PowerPoint.Slide slide, PowerPoint.Shape shape, string fileName)
        {
            PowerPoint.Shape oleObj;
            oleObj = slide.Shapes.AddOLEObject(Left: shape.Left, Top: shape.Top, Width: shape.Width, Height: shape.Height,FileName:fileName);
            oleObj.Name = shape.Name;
        }

  添加OLE调用:(OLE识别符:http://msdn.microsoft.com/zh-cn/library/ff746158.aspx)

string filePath = "E:\\FF.doc";
                filePath = "E:\\FF.ppt";
                filePath = "E:\\FF.xls";

                filePath = @"C:\Users\Administrator\Videos\广联达PB043926.AVI";
                filePath = @"C:\Users\Administrator\Videos\广联达轻轻的问候.swf";

                //AddMedia(slide, filePath);

                AddOLEDPath(slide, filePath);

                string className = "Excel.Sheet";
                className = "Word.Document";
                className = "PowerPoint.Slide";
                // AddOLEDClass(slide, className);

  4.对于幻灯片中shapes的遍历处理:(可以根据不同的业务需要对对应的幻灯片 中的一些形状进行处理)

PowerPoint.Slides slides = Application.ActivePresentation.Slides;//获取当前演示文稿所有幻灯片
            if (!IsHandler(slides))//所有对象是否处理过
            {
                for (int i = 1; i <= slides.Count; i++)
                {
                    PowerPoint.Slide slide = slides[i];
                    PowerPoint.Shapes shapes = slide.Shapes;
                    int count = shapes.Count;//元素集合总数是变化的
                    for (int j = 1; j <= count; j++)
                    {
                        if (shapes[i].Name.Contains("PPT"))
                        {
                            shapes[j].Visible = Office.MsoTriState.msoFalse;//将其隐藏
                            string picPath = "c:\\AD.jpg";//
                            AddPicture(slide, shapes[j], picPath);//替换图片
                        }
                    }
                }
            }
 private void AddPicture(PowerPoint.Slide slide, PowerPoint.Shape shape, string filePath)
        {
            PowerPoint.Shape pic;
            pic = slide.Shapes.AddPicture(filePath, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, shape.Left, shape.Top, shape.Width, shape.Height);
            pic.Name = "AD" + shape.Name;
        }

  

三、相关资料参考

MSDN参考资料:
PowerPoint 2013 开发
http://msdn.microsoft.com/zh-cn/library/office/fp161225.aspx

PowerPoint 2013 开发人员参考 (机器翻译)
http://msdn.microsoft.com/zh-cn/library/office/ee861525.aspx

PowerPoint 2013对象模型引用
http://msdn.microsoft.com/zh-cn/library/office/ee861525.aspx

PowerPoint 解决方案
http://msdn.microsoft.com/zh-cn/library/vstudio/bb772069.aspx

 

相关博客资料:
C# PowerPoint操作的基本用法
http://www.cnblogs.com/hyruur/archive/2011/02/14/1954118.html

VSTO简介及其部署
http://blog.csdn.net/v_jzho/article/category/339472

我的VSTO之路系列
http://www.cnblogs.com/izualx/tag/VSTO/

探索 Word 2007 开发
http://www.cnblogs.com/allenlooplee/category/104575.html

Excel 二次开发系列
http://www.cnblogs.com/tomin/category/209011.html

VSTO项目开发
http://www.cnblogs.com/2018/category/249767.html

VSTO对象操作
http://blog.csdn.net/tianyu0910/article/category/703094

VSTO学习笔记
http://www.cnblogs.com/brooks-dotnet/category/233027.html

标签:http,自定义,Office,slide,shape,添加,二次开发,PowerPoint
来源: https://www.cnblogs.com/guangzhiruijie/p/15972048.html