1. 程式人生 > 其它 >對於VSTO中PPT中程式碼新增文字和圖片

對於VSTO中PPT中程式碼新增文字和圖片

   private void AddTextBox(PowerPoint.Slide slide, string txtContent)
        {
            PowerPoint.Shape textbox;
            textbox = slide.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, 50, 100, 600, 50);//向當前PPT新增文字框
            textbox.TextFrame.TextRange.Text = txtContent;//設定文字框的內容
            textbox.TextFrame.TextRange.Font.Size = 48;//設定文字字型大小
            textbox.TextFrame.TextRange.Font.Color.RGB = Color.DarkViolet.ToArgb();//設定文字顏色
        }

        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 = "廣告圖片";
            pic.Height = shape.Height;
            pic.Width = shape.Width;
        }

        private void AddTextMessage(PowerPoint.Slide slide, PowerPoint.Shape shape, string txtContent)
        {
            PowerPoint.Shape textbox;
            textbox = slide.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, shape.Left, shape.Top, shape.Width, shape.Height);//向當前PPT新增文字框
            textbox.Height = shape.Height;
            textbox.Width = shape.Width;
            textbox.TextFrame.TextRange.Text = txtContent;
            textbox.TextFrame.TextRange.Font.Color.RGB = Color.Orange.ToArgb();
        }

  

/// <summary>
        /// 新增圖片
        /// </summary>
        /// <param name="slide"></param>
        /// <param name="filePath"></param>
        private void AddADPicture(PowerPoint.Slide slide, string filePath)
        {
            PowerPoint.Shape pic;
            pic = slide.Shapes.AddPicture(filePath, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, 100, 200, 100, 100);
            pic.Name = "GlodonAD";
        }

        /// <summary>
        /// 新增多媒體檔案
        /// </summary>
        /// <param name="slide"></param>
        /// <param name="filePath"></param>
        private void AddMedia(PowerPoint.Slide slide, string filePath)
        {
            PowerPoint.Shape media;
            media = slide.Shapes.AddMediaObject(filePath, 10, 10, 200, 200);
            media.Name = "Media";
        }

		//對於OLED物件標識:http://msdn.microsoft.com/zh-cn/library/office/ff746158.aspx
		
        /// <summary>
        /// 通過ClassName建立物件
        /// </summary>
        /// <param name="slide"></param>
        /// <param name="className"></param>
        private void AddOLEDClass(PowerPoint.Slide slide, string className)
        {
            PowerPoint.Shape oledClass;
            oledClass = slide.Shapes.AddOLEObject(50, 50, 800, 500, ClassName: className, DisplayAsIcon: Office.MsoTriState.msoTrue);
            oledClass.Name = "OLEDClassName";
        }

        /// <summary>
        /// 通過檔案路徑,新增檔案
        /// </summary>
        /// <param name="slide"></param>
        /// <param name="filePath"></param>
        private void AddOLEDPath(PowerPoint.Slide slide, string filePath)
        {
            PowerPoint.Shape oledFile;
            oledFile = slide.Shapes.AddOLEObject(50, 50, 800, 500, FileName: filePath, DisplayAsIcon: Office.MsoTriState.msoTrue);//, DisplayAsIcon: Office.MsoTriState.msoTrue
            oledFile.Name = "OLEDFilePath";
        }

  

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);

  好像對於滑鼠型別和鍵盤型別的標識有誤(鍵盤應該是2,滑鼠應該是7;好像以前是13和14?)

public enum WH_Codes : int
        {
            //底層鍵盤鉤子
            WH_KEYBOARD_LL = 2,//13

            //底層滑鼠鉤子
            WH_MOUSE_LL = 7, //14

            //nCode為0
            HC_ACTION=0
        }