本篇分享對於Power Point中一些命令的操作和對於一些比較常用對象、特殊對象的添加功能。
對於Power Point命令操作:
有了前一篇《[轉]VSTO Office二次開發RibbonX代碼結構》的了解,就可以嘗試現實自己的RibbonX的相關元素的操作了,這里提供簡單的小示例:
1.創建外接程序。
創建一個PPT的外接程序,在《VSTO Office二次開發對PowerPoint功能簡單測試》隨筆中有介紹,如何創建一個簡單的PPT外接程序。
2.創建功能區(XML)。
在項目右鍵單擊添加,彈出“添加新項”,添加“功能區(XML)”,以XML的形式創建功能區。
在Ribbon1.xml添加如下代碼,定義三個回調事件"MyCopy"\"MyCut"\"GetInfo":
<?xml version="1.0" encoding="UTF-8"?> <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load"> <commands> <!--PPT中的文件保存功能不能使用--> <command idMso="FileSave" enabled="false"/> <!--"MyCut"擴展PPT中"剪切"命令的響應事件--> <command idMso="Cut" enabled="true" onAction="MyCut"/> <command idMso="Copy" enabled="true" onAction="MyCopy"/> </commands> <ribbon> <tabs> <tab idMso="TabAddIns"> <group id="MyGroup" label="My Group"> <button id="MyButton" label="MyButton" onAction="GetInfo"/> </group> </tab> </tabs> </ribbon> </customUI>
3.回調函數編寫。
在Ribbon1.cs文件中添加對於回調函數的處理代碼:
/// <summary> /// MyButton的回調方法 /// </summary> /// <param name="control"></param> public void GetInfo(Office.IRibbonControl control) { MessageBox.Show("Released"); } /// <summary> /// MyCopy的回調方法 /// </summary> /// <param name="control">觸發控件</param> /// <param name="cancelDefault">是否取消功能</param> public void MyCopy(Office.IRibbonControl control, ref bool cancelDefault) { bool isSuccess = IsSuccess(); //如果cancelDefault返回false,則取消該操作 cancelDefault = isSuccess; } /// <summary> /// 要處理的業務 /// </summary> /// <returns></returns> private bool IsSuccess() { return false; }
4.關聯外接程序。
前幾步把Ribbon設計完畢,現在與Power Point外接程序進行關聯:(重寫RequestService方法)
private Ribbon1 ribbon;//重新定義Ribbon1 /// <summary> /// 重寫RequestService方法 /// </summary> /// <param name="serviceGuid"></param> /// <returns></returns> protected override object RequestService(Guid serviceGuid) { if (serviceGuid == typeof(Office.IRibbonExtensibility).GUID) { if (ribbon == null) ribbon = new Ribbon1(); return ribbon; } return base.RequestService(serviceGuid); }
5.功能測試。
F5運行程序,簡單測試。對於“文件保存”、“剪切”、“復制”操作的測試。
對於操作的RibbonX的Office 2010所有命令(Office2010ControlIDs.exe也有對於2007支持的命令):
參見:http://social.msdn.microsoft.com/Forums/pl/worddev/thread/337946b2-edc4-4f40-bb45-1babf58a5e7e
下載:http://www.microsoft.com/download/en/details.aspx?id=6627
對於RibbonX進行對Office程序的XML編程很不錯。幾個可參考資源:
使用 Open XML 文件格式自定義 Office Fluent 功能區
利用您自己的功能區選項卡和控件擴展 2007 Office System
細品RibbonX(12):使用XML Notepad自定義功能區
http://www.360doc.com/content/09/1110/11/406571_8725398.shtml
對於PowerPoint添加對象:
微軟也提供了一些調用接口:
《Shapes.AddPicture 方法 》http://msdn.microsoft.com/zh-cn/library/office/jj735114.aspx
《OLE 程序標識符 (PowerPoint)》http://msdn.microsoft.com/zh-cn/library/office/ff746158.aspx
常用對象添加調用代碼:
/// <summary> /// 添加常用對象 /// 通過OLE添加對象,支持網絡文件訪問 /// 添加公式:Equation.3 /// 添加圖表:MSGraph.Chart /// </summary> /// <param name="slide">幻燈片</param> /// <param name="filePath">文件路徑</param> /// <param name="left">居左位置</param> /// <param name="top">居右位置</param> /// <param name="width">寬度</param> /// <param name="height">高度</param> #region 添加對象操作 //添加圖片 private void AddADPicture(PowerPoint.Slide slide, string filePath, float left, float top, float width, float height) { PowerPoint.Shape pic; pic = slide.Shapes.AddPicture(filePath, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue,left,top,width,height); pic.Name = "Picture"; pic.Tags.Add("Name", "Value");//或用tag標簽攜帶數據 } //添加媒體文件 private void AddMedia(PowerPoint.Slide slide, string filePath, float left, float top, float width, float height) { PowerPoint.Shape media; media = slide.Shapes.AddMediaObject(filePath,left,top,width,height); media.Name = "Media"; } //添加任意對象,但需要激活操作 private void AddOLEDPath(PowerPoint.Slide slide, string filePath, float left, float top, float width, float height) { PowerPoint.Shape oledFile; oledFile = slide.Shapes.AddOLEObject(left,top,height,width,FileName: filePath, DisplayAsIcon: Office.MsoTriState.msoTrue);//, DisplayAsIcon: Office.MsoTriState.msoTrue oledFile.Name = filePath; } //添加Flash播放器,需要本機上安裝Flash播放插件 private void AddFlashObj(PowerPoint.Slide slide, string fileName, float left, float top, float width, float height) { object oleControl = slide.Shapes.AddOLEObject(left, top, width, height, "ShockwaveFlash.ShockwaveFlash", "", Office.MsoTriState.msoFalse, "", 0, "", Office.MsoTriState.msoFalse).OLEFormat.Object; Type oleControlType = oleControl.GetType(); /* 設置flash播放屬性 */ oleControlType.InvokeMember("EmbedMovie", BindingFlags.SetProperty, null, oleControl, new object[] { true }); oleControlType.InvokeMember("Playing", BindingFlags.SetProperty, null, oleControl, new object[] { true }); oleControlType.InvokeMember("Movie", BindingFlags.SetProperty, null, oleControl, new object[] { fileName });// 設置Flash文件路徑 oleControlType.InvokeMember("Scale", BindingFlags.SetProperty, null, oleControl, new object[] { "ExactFit" });//設置顯示比例為:嚴格匹配 oleControlType.InvokeMember("ScaleMode", BindingFlags.SetProperty, null, oleControl, new object[] { 2 }); } //添加3D模型播放器,需要本機上安裝BS Contact插件 private void AddBSContactObj(PowerPoint.Slide slide, string fileName, float left, float top, float width, float height) { object oleControl = slide.Shapes.AddOLEObject(left, top, width, height, "BSContact.BSContact", "", Office.MsoTriState.msoFalse, "", 0, "", Office.MsoTriState.msoFalse).OLEFormat.Object; Type oleControlType = oleControl.GetType(); /* 設置BSContact播放屬性 */ oleControlType.InvokeMember("Enabled", BindingFlags.SetProperty, null, oleControl, new object[] { true }); oleControlType.InvokeMember("url", BindingFlags.SetProperty, null, oleControl, new object[] { fileName });// 設置BSContact文件路徑 oleControlType.InvokeMember("walkSpeed", BindingFlags.SetProperty, null, oleControl, new object[] { 0 }); oleControlType.InvokeMember("animateAllViewpoints", BindingFlags.SetProperty, null, oleControl, new object[] { false }); } //添加圖片,以OLE對象形式 private void AddOlePicture(PowerPoint.Slide slide, string fileName, float left, float top, float width, float height) { object oleControl = slide.Shapes.AddOLEObject(left, top, width, height, "Forms.Image.1", "", Office.MsoTriState.msoFalse, "", 0, "", Office.MsoTriState.msoFalse).OLEFormat.Object; Type oleControlType = oleControl.GetType(); oleControlType.InvokeMember("Enabled", BindingFlags.SetProperty, null, oleControl, new object[] { true }); Bitmap bitmap = new Bitmap(fileName); stdole.StdPicture pic = (stdole.StdPicture)ImageConverter2.ImageToIpicture(bitmap); oleControlType.InvokeMember("Picture", BindingFlags.SetProperty, null, oleControl, new object[] { pic }); } //添加Windows Media Player播放器,本機安裝Windows Media Player(插件) private void AddOleMedia(PowerPoint.Slide slide, string fileName, float left, float top, float width, float height) { //MediaPlayer.MediaPlayer.1或WMPlayer.OCX.7 object oleControl = slide.Shapes.AddOLEObject(left, top, width, height, "WMPlayer.OCX.7", "", Office.MsoTriState.msoFalse, "", 0, "", Office.MsoTriState.msoFalse).OLEFormat.Object; Type oleControlType = oleControl.GetType(); oleControlType.InvokeMember("Enabled", BindingFlags.SetProperty, null, oleControl, new object[] { true }); oleControlType.InvokeMember("Url", BindingFlags.SetProperty, null, oleControl, new object[] {fileName }); } //添加Form窗體,窗體中添加Image控件,單擊彈出"PPT"信息提示 //命名引用:using MF = Microsoft.Vbe.Interop.Forms;
private void AddOleForm(PowerPoint.Slide slide, string fileName, float left, float top, float width, float height) { var oleControl = slide.Shapes.AddOLEObject(left, top, width, height, "Forms.Frame.1", "", Office.MsoTriState.msoFalse, "", 0, "", Office.MsoTriState.msoFalse); var obj = oleControl.OLEFormat.Object; oleControl.Name = "Frame"; PowerPoint.OLEFormat oleF = slide.Shapes.Range("Frame").OLEFormat; MF.Frame frm1 = (MF.Frame)oleF.Object; frm1.Caption = "PP"; MF.Image image = (MF.Image)frm1.Controls.Add("Forms.Image.1"); image.Click += new MF.ImageEvents_ClickEventHandler(image_Click); Bitmap bitmap = new Bitmap(fileName); image.Picture = (stdole.StdPicture)ImageConverter2.ImageToIpicture(bitmap); } void image_Click() { MessageBox.Show("PPT"); } #endregion
其中,圖片轉化類,將StdPicture轉換為Bitmap:
public class ImageConverter2 : System.Windows.Forms.AxHost { public ImageConverter2() : base("59EE46BA-677D-4d20-BF10-8D8067CB8B33") { } public static stdole.IPictureDisp ImageToIpicture(System.Drawing.Image image) { return (stdole.IPictureDisp)ImageConverter2.GetIPictureDispFromPicture(image); } public static System.Drawing.Image IPictureToImage(stdole.StdPicture picture) { return ImageConverter2.GetPictureFromIPicture(picture); } }
也可以添加Word\Excel\PPT《VSTO Office二次開發對PPT自定義任務窗格測試》,發現通過添加OLE對象使用ClassName標識的形式,可以添加本機安裝的很多Com組件,在PPT中插入的ActiveX組件基本都可以通過OLE對象通過程序添加到PPT中。