欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#操作PowerPoint的方法

 更新時(shí)間:2015年04月01日 12:28:47   作者:重負(fù)在身  
這篇文章主要介紹了C#操作PowerPoint的方法,涉及C#針對(duì)PowerPoint的打開、讀取、播放等技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#操作PowerPoint的方法。分享給大家供大家參考。具體如下:

這里C#操作PowerPoint的基本代碼,包括打開ppt文件、讀取幻燈頁,插入幻燈片,自動(dòng)播放等

using System;
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文檔操作實(shí)現(xiàn)類.
  /// </summary>
  public class OperatePPT
  {
    #region=========基本的參數(shù)信息=======
    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)
    {
      //防止連續(xù)打開多個(gè)PPT程序.
      if (this.objApp != null) { return; }
      try
      {
        objApp = new POWERPOINT.Application();
        //以非只讀方式打開,方便操作結(jié)束后保存.
        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>
    /// 自動(dòng)播放PPT文檔.
    /// </summary>
    /// <param name="filePath">PPTy文件路徑.</param>
    /// <param name="playTime">翻頁的時(shí)間間隔.【以秒為單位】</param>
    public void PPTAuto(string filePath, int playTime)
    {
      //防止連續(xù)打開多個(gè)PPT程序.
      if (this.objApp != null) { return; }
      objApp = new POWERPOINT.Application();
      objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoCTrue, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);
      // 自動(dòng)播放的代碼(開始)
      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;
      //設(shè)置翻頁的時(shí)間.
      objSST.AdvanceOnTime = OFFICECORE.MsoTriState.msoCTrue;
      objSST.AdvanceTime = playTime;
      //翻頁時(shí)的特效!
      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>
    /// 對(duì)當(dāng)前的PPT頁面進(jìn)行圖片插入操作。
    /// </summary>
    /// <param name="alImage">圖片對(duì)象信息數(shù)組</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>
    /// 計(jì)算InkCanvas畫板上的偏移參數(shù),與PPT上顯示圖片的參數(shù)。
    /// 用于PPT加載圖片時(shí)使用
    /// </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>
    /// 關(guān)閉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
  }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C#實(shí)現(xiàn)簡化QQ聊天窗口

    C#實(shí)現(xiàn)簡化QQ聊天窗口

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡化QQ聊天窗口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#窗體傳值實(shí)例匯總

    C#窗體傳值實(shí)例匯總

    這篇文章主要介紹了C#窗體傳值,實(shí)例形式匯總了靜態(tài)變量傳值、委托傳值、對(duì)話框之間的傳值等常見應(yīng)用技巧,需要的朋友可以參考下
    2014-12-12
  • C#實(shí)現(xiàn)撲克游戲(21點(diǎn))的示例代碼

    C#實(shí)現(xiàn)撲克游戲(21點(diǎn))的示例代碼

    21點(diǎn)又名黑杰克,該游戲由2到6個(gè)人玩,使用除大小王之外的52張牌,游戲者的目標(biāo)是使手中的牌的點(diǎn)數(shù)之和不超過21點(diǎn)且盡量大。本文將用C#實(shí)現(xiàn)這一經(jīng)典游戲,需要的可以參考一下
    2022-08-08
  • WPF通過使用C#代碼給控件賦值

    WPF通過使用C#代碼給控件賦值

    開發(fā)中偶爾會(huì)用到使用c#代碼給控件賦值,這里需要通過F12找到控件的賦值類型,然后在看控件可以使用那種方式賦值,花點(diǎn)時(shí)間研究一下,基本都可以通過c#代碼給控件賦值的,需要的朋友可以參考下
    2023-09-09
  • C# lambda表達(dá)式應(yīng)用如何找出元素在list中的索引

    C# lambda表達(dá)式應(yīng)用如何找出元素在list中的索引

    這篇文章主要介紹了C# lambda表達(dá)式應(yīng)用如何找出元素在list中的索引的相關(guān)資料,需要的朋友可以參考下
    2018-01-01
  • C# 字符串與unicode互相轉(zhuǎn)換實(shí)戰(zhàn)案例

    C# 字符串與unicode互相轉(zhuǎn)換實(shí)戰(zhàn)案例

    這篇文章主要介紹了C# 字符串與unicode互相轉(zhuǎn)換實(shí)戰(zhàn)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • C#實(shí)現(xiàn)MQTT服務(wù)端與客戶端通訊功能

    C#實(shí)現(xiàn)MQTT服務(wù)端與客戶端通訊功能

    這篇文章介紹了C#實(shí)現(xiàn)MQTT服務(wù)端與客戶端通訊的功能,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • 詳解C# Protobuf如何做到0分配內(nèi)存的序列化

    詳解C# Protobuf如何做到0分配內(nèi)存的序列化

    這篇文章主要介紹了詳解C# Protobuf如何做到0分配內(nèi)存的序列化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • 解析OpenXml?Pptx的邊框虛線轉(zhuǎn)為WPF的邊框虛線問題

    解析OpenXml?Pptx的邊框虛線轉(zhuǎn)為WPF的邊框虛線問題

    這篇文章主要介紹了OpenXml?Pptx的邊框虛線轉(zhuǎn)為WPF的邊框虛線,在文中用PPTX的7種直線,分別設(shè)置7種能夠設(shè)置的虛線類型,具體實(shí)例代碼跟隨小編一起看看吧
    2021-12-12
  • 深入探討C#中的結(jié)構(gòu)struct

    深入探討C#中的結(jié)構(gòu)struct

    這篇文章詳細(xì)介紹了C#中的結(jié)構(gòu)struct,有需要的朋友可以參考一下
    2013-09-09

最新評(píng)論