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.Forms;
using System.IO; using System.ComponentModel;
using System.Data;
using System.Drawing; namespace pptWrite
{ /// <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; #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();
objApp.Visible = OFFICECORE.MsoTriState.msoTrue; //以非只读方式打开,方便操作结束后保存.
objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoFalse); }
catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message.ToString());
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 = ; i < Slides; i++) { SlideIdx[i] = i + ; };
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 = ;
objSSS.EndingSlide = Slides;
objSSS.Run();
//Wait for the slide show to end.
objSSWs = objApp.SlideShowWindows;
while (objSSWs.Count >= ) System.Threading.Thread.Sleep(playTime * );
this.objPresSet.Close();
this.objApp.Quit();
}
/// <summary>
/// PPT下一页。
/// </summary>
public void NextSlide()
{
if (this.objApp != null)
try
{
this.objPresSet.SlideShowWindow.View.Next();
}
catch
{ }
}
/// <summary>
/// PPT上一页。
/// </summary>
public void PreviousSlide()
{
if (this.objApp != null)
this.objPresSet.SlideShowWindow.View.Previous();
} private int PageNum()
{
return objPresSet.Slides.Count; } public void SetLine()
{
int num = PageNum();
for (int i = ; i < num; i++)
{
if (i > )
{
objSldRng= objPresSet.Slides.Range(i);
objSldRng.Select();
try
{
objSldRng.Application.ActiveWindow.Selection.SlideRange.Shapes.SelectAll();
objSldRng.Application.ActiveWindow.Selection.ShapeRange.Line.Visible = OFFICECORE.MsoTriState.msoFalse;
}
catch
{ }
//MessageBox.Show("" + i.ToString()); //NextSlide();
} } } /// <summary>
/// 关闭PPT文档。
/// </summary>
public void PPTClose()
{
//装备PPT程序。
if (this.objPresSet != null)
{
//判断是否退出程序,可以不使用。
//objSSWs = objApp.SlideShowWindows;
//if (objSSWs.Count >= 1)
//{
//if (MessageBox.Show("是否保存修改的笔迹!", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
this.objPresSet.Save();
//}
//this.objPresSet.Close();
}
if (this.objApp != null)
this.objApp.Quit();
GC.Collect();
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace pptWrite
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
ylbasicpub.YLCover.selectfilename(this.textBox1, "*.ppt|*.ppt");
} private void Form1_Load(object sender, EventArgs e)
{
ylbasicpub.YLCover.loadXml(this);
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
ylbasicpub.YLCover.SaveXml(this);
} private void button2_Click(object sender, EventArgs e)
{
OperatePPT pOperatePPT = new OperatePPT();
pOperatePPT.PPTOpen(this.textBox1.Text);
pOperatePPT.SetLine();
//pOperatePPT.PPTClose(); }
}
}
05-11 22:16
查看更多