c#如何操作ppt的播放

來源:互聯網
上載者:User

標籤:c# ppt 代碼

C#控制ppt的代碼

包括開啟ppt檔案、讀取幻燈頁,插入投影片,自動播放等

using System.Collections.Generic;

using System.Linq;

using System.Text;

using OFFICECORE = Microsoft.Office.Core;

using POWERPOINT = Microsoft.Office.Interop.PowerPoint;

using System.windows;

using System.Collections;

using System.windows.Controls;

namespace PPTDraw.PPTOperate

{

  /// <summary>

  /// PPT文檔操作實作類別.

  /// </summary>

  public class OperatePPT

  {

    #region=========基本的參數資訊=======

    POWERPOINT.Application objApp = null;

    POWERPOINT.Presentation objPresSet = null;

    POWERPOINT.SlideShowwindows objSSWs;

    POWERPOINT.SlideShowTransition objSST;

    POWERPOINT.SlideShowSettings objSSS;

    POWERPOINT.SlideRange objSldRng;

    bool bAssistantOn;

    double pixperPoint = 0;

    double offsetx = 0;

    double offsety = 0;

    #endregion

    #region===========操作方法==============

    /// <summary>

    /// 開啟PPT文檔並播放顯示。

    /// </summary>

    /// <param name="filePath">PPT檔案路徑</param>

    public void PPTOpen(string filePath)

    {

      //防止連續開啟多個PPT程式.

      if (this.objApp != null) { return; }

      try

      {

        objApp = new POWERPOINT.Application();

        //以非唯讀方式開啟,方便操作結束後儲存.

        objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);

        //Prevent Office Assistant from displaying alert messages:

        bAssistantOn = objApp.Assistant.On;

        objApp.Assistant.On = false;

        objSSS = this.objPresSet.SlideShowSettings;

        objSSS.Run();

      }

      catch (Exception ex)

      {

        this.objApp.Quit();

      }

    }

    /// <summary>

    /// 自動播放PPT文檔.

    /// </summary>

    /// <param name="filePath">PPTy檔案路徑.</param>

    /// <param name="playTime">翻頁的時間間隔.【以秒為單位】</param>

    public void PPTAuto(string filePath, int playTime)

    {

      //防止連續開啟多個PPT程式.

      if (this.objApp != null) { return; }

      objApp = new POWERPOINT.Application();

      objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoCTrue, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);

      // 自動播放的代碼(開始)

      int Slides = objPresSet.Slides.Count;

      int[] SlideIdx = new int[Slides];

      for (int i = 0; i < Slides; i++) { SlideIdx[i] = i + 1; };

      objSldRng = objPresSet.Slides.Range(SlideIdx);

      objSST = objSldRng.SlideShowTransition;

      //設定翻頁的時間.

      objSST.AdvanceOnTime = OFFICECORE.MsoTriState.msoCTrue;

      objSST.AdvanceTime = playTime;

      //翻頁時的特效!

      objSST.EntryEffect = POWERPOINT.PpEntryEffect.ppEffectCircleOut;

      //Prevent Office Assistant from displaying alert messages:

      bAssistantOn = objApp.Assistant.On;

      objApp.Assistant.On = false;

      //Run the Slide show from slides 1 thru 3.

      objSSS = objPresSet.SlideShowSettings;

      objSSS.StartingSlide = 1;

      objSSS.EndingSlide = Slides;

      objSSS.Run();

      //Wait for the slide show to end.

      objSSWs = objApp.SlideShowwindows;

      while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(playTime * 100);

      this.objPresSet.Close();

      this.objApp.Quit();

    }

    /// <summary>

    /// PPT下一頁。

    /// </summary>

    public void NextSlide()

    {

      if (this.objApp != null)

        this.objPresSet.SlideShowwindow.View.Next();

    }

    /// <summary>

    /// PPT上一頁。

    /// </summary>

    public void PreviousSlide()

    {

      if (this.objApp != null)

        this.objPresSet.SlideShowwindow.View.Previous();

    }

    /// <summary>

    /// 對當前的PPT頁面進行圖片插入操作。

    /// </summary>

    /// <param name="alImage">圖片對象資訊數組</param>

    /// <param name="offsetx">插入圖片距離左邊長度</param>

    /// <param name="pixperPoint">距離比例值</param>

    /// <returns>是否添加成功!</returns>

    public bool InsertToSlide(List<PPTOBJ> listObj)

    {

      bool InsertSlide = false;

      if (this.objPresSet != null)

      {

        this.SlideParams();

        int slipeint = objPresSet.SlideShowwindow.View.CurrentShowPosition;

        foreach (PPTOBJ myobj in listObj)

        {

          objPresSet.Slides[slipeint].Shapes.AddPicture(

             myobj.Path,      //圖片路徑

             OFFICECORE.MsoTriState.msoFalse,

             OFFICECORE.MsoTriState.msoTrue,

             (float)((myobj.X - this.offsetx) / this.pixperPoint),    //插入圖片距離左邊長度

             (float)(myobj.Y / this.pixperPoint),    //插入圖片距離頂部高度

             (float)(myobj.Width / this.pixperPoint),  //插入圖片的寬度

             (float)(myobj.Height / this.pixperPoint)  //插入圖片的高度

           );

        }

        InsertSlide = true;

      }

      return InsertSlide;

    }

    /// <summary>

    /// 計算InkCanvas畫板上的位移參數,與PPT上顯示圖片的參數。

    /// 用於PPT載入圖片時使用

    /// </summary>

    private void SlideParams()

    {

      double slideWidth = this.objPresSet.PageSetup.SlideWidth;

      double slideHeight = this.objPresSet.PageSetup.SlideHeight;

      double inkCanWidth = SystemParameters.PrimaryScreenWidth;//inkCan.ActualWidth;

      double inkCanHeight = SystemParameters.PrimaryScreenHeight;//inkCan.ActualHeight ;

      if ((slideWidth / slideHeight) > (inkCanWidth / inkCanHeight))

      {

        this.pixperPoint = inkCanHeight / slideHeight;

        this.offsetx = 0;

        this.offsety = (inkCanHeight - slideHeight * this.pixperPoint) / 2;

      }

      else

      {

        this.pixperPoint = inkCanHeight / slideHeight;

        this.offsety = 0;

        this.offsetx = (inkCanWidth - slideWidth * this.pixperPoint) / 2;

      }

    }

    /// <summary>

    /// 關閉PPT文檔。

    /// </summary>

    public void PPTClose()

    {

      //裝備PPT程式。

      if (this.objPresSet != null)

      {

        //判斷是否退出程式,可以不使用。

        //objSSWs = objApp.SlideShowwindows;

        //if (objSSWs.Count >= 1)

        //{

          if (MessageBox.Show("是否儲存修改的筆跡!", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)

            this.objPresSet.Save();

        //}

        //this.objPresSet.Close();

      }

      if (this.objApp != null)

        this.objApp.Quit();

      GC.Collect();

    }

    #endregion

  }

}


本文出自 “我的筆記” 部落格,請務必保留此出處http://9891814.blog.51cto.com/9881814/1627298

c#如何操作ppt的播放

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.