The last article made a simple test on the PPT function of VSTO Office secondary development operation, mainly about how to create an add-on and add custom text information when creating a PPT slide, how to simply customize the task pane and how to add a visual function area. We should have a simple understanding of how VSTO operates ppt, This article shares the relevant tests for customizing the task pane.
1, Ribbon buttons control the display and hiding of CustomTaskPane
1. Create an add in, add the visual function area and user-defined control, and add two buttons in the visual function area, one to control the display of the custom task pane and the other to control the hiding of the custom task pane.
2. First associate the custom task pane with the add in, and click thisaddin Add the following code to the CS file:
public CustomTaskPane myCustomTaskPanel = null;//custom private void ThisAddIn_Startup(object sender, System.EventArgs e) { AddTaskPane();//Add custom Task Pane } /// <summary> ///Add a custom Task Pane /// </summary> private void AddTaskPane() { UCTaskPane taskPane = new UCTaskPane();//Custom user control name myCustomTaskPanel = this.CustomTaskPanes.Add(taskPane, "My Task Pane"); myCustomTaskPanel.Width = 200; myCustomTaskPanel.Visible = true; }
3. Add the following code to the two buttons in the visualization ribbon:
/// <summary> ///Open the custom task pane /// </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> ///Close the custom task pane /// </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 run to see the effect. You can also add a toggle button in the visualization function area and add a line of code in the button click event, which can also well control the display and hiding of the task pane:
Globals.ThisAddIn.myCustomTaskPanel.Visible = toggleBtn.Checked;
Mainly how to call the custom task pane: globals ThisAddIn. Mycustomtaskpanel, and then control it (custom task pane can well realize the function of material library rendering similar to WPS).
2, Customize the actions of the task pane on slides
Function: in the custom task pane, add a date control to realize the relevant operations on the corresponding slides in the PPT through its DateChanged event, such as adding text, adding pictures and adding multimedia objects
1. Add a user-defined control and a date control, and add the following code in its DateChanged event:
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;//Get all PPT documents for the current application for (int i = 1; i <= slides.Count; i++)//Traverse the document collection and add a text box { PowerPoint.Slide slide = slides[i]; textbox = slide.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, 50, 100, 600, 50);//Add text box to current PPT textbox.TextFrame.TextRange.Text = textContent;//Set the contents of the text box textbox.TextFrame.TextRange.Font.Size = 48;//Set text font size textbox.TextFrame.TextRange.Font.Color.RGB = Color.DarkViolet.ToArgb();//Set text color } }
2.F5 run, click the date, and a TextBox object will be added to each slide in the PPT, with the position (50 on the left, 100 on the top) and size (600 in width and 50 in height).
Mainly how to get all the slides of the current presentation: globals ThisAddIn. Application. ActivePresentation. Slides
3. OLE extension: add text object
/// <summary> ///Add TextBox text box /// </summary> ///< param name = "slide" > slide to add text box < / param > ///< param name = "textcontent" > text box display content < / param > private void AddTextBox(PowerPoint.Slide slide, string textContent) { PowerPoint.Shape textbox; textbox = slide.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, 50, 100, 600, 50);//Add text box to current PPT textbox.TextFrame.TextRange.Text = textContent;//Set the contents of the text box textbox.TextFrame.TextRange.Font.Size = 48;//Set text font size textbox.TextFrame.TextRange.Font.Color.RGB = Color.DarkViolet.ToArgb();//Set text color } /// <summary> ///Add picture /// </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> ///Add audio \ video files /// </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> ///Add OLE objects through file paths /// </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> ///Adding OLE objects through ClassName /// </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; }
Add ole call: (OLE ID: 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\Guanglianda PB043926.AVI"; filePath = @"C:\Users\Administrator\Videos\Guanglianda's gentle greetings.swf"; //AddMedia(slide, filePath); AddOLEDPath(slide, filePath); string className = "Excel.Sheet"; className = "Word.Document"; className = "PowerPoint.Slide"; // AddOLEDClass(slide, className);
4. Traversal of shapes in slides: (some shapes in corresponding slides can be processed according to different business needs)
PowerPoint.Slides slides = Application.ActivePresentation.Slides;//Get all slides of the current presentation if (!IsHandler(slides))//Have all objects been processed { for (int i = 1; i <= slides.Count; i++) { PowerPoint.Slide slide = slides[i]; PowerPoint.Shapes shapes = slide.Shapes; int count = shapes.Count;//The total number of element sets varies for (int j = 1; j <= count; j++) { if (shapes[i].Name.Contains("PPT")) { shapes[j].Visible = Office.MsoTriState.msoFalse;//Hide it string picPath = "c:\\AD.jpg";// AddPicture(slide, shapes[j], picPath);//Replace picture } } } } 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; }
3, Relevant references
MSDN reference:
PowerPoint 2013 development
http://msdn.microsoft.com/zh-cn/library/office/fp161225.aspx
PowerPoint 2013 developer reference (machine translation)
http://msdn.microsoft.com/zh-cn/library/office/ee861525.aspx
PowerPoint 2013 object model reference
http://msdn.microsoft.com/zh-cn/library/office/ee861525.aspx
PowerPoint solution
http://msdn.microsoft.com/zh-cn/library/vstudio/bb772069.aspx
Related blog materials:
Basic usage of C# PowerPoint operation
http://www.cnblogs.com/hyruur/archive/2011/02/14/1954118.html
Introduction and deployment of VSTO
http://blog.csdn.net/v_jzho/article/category/339472
My VSTO road series
http://www.cnblogs.com/izualx/tag/VSTO/
Explore Word 2007 development
http://www.cnblogs.com/allenlooplee/category/104575.html
Excel secondary development series
http://www.cnblogs.com/tomin/category/209011.html
VSTO project development
http://www.cnblogs.com/2018/category/249767.html
VSTO object operation
http://blog.csdn.net/tianyu0910/article/category/703094
VSTO learning notes
http://www.cnblogs.com/brooks-dotnet/category/233027.html