最近做个项目,要求在winform中自动播放ppt,在网上查了很多资料,都不能自动,后来自己花了些时间研究了下,终于可以自动播放了,和大家分享下,程序还有待改善,感觉播放导入有点慢,ppt换页样式不能设置,还请高手指点了。
1.导入如下注册表值
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.Show.8]
"BrowserFlags"=dword:800000A0
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.Show.12]
"BrowserFlags"=dword:800000A0
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.ShowMacroEnabled.12]
"BrowserFlags"=dword:800000A0
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.SlideShow.8]
"BrowserFlags"=dword:800000A0
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.SlideShow.12]
"BrowserFlags"=dword:800000A0
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.SlideShowMacroEnabled.12]
"BrowserFlags"=dword:800000A0
2.创建一个winform,在设计视图拖入axWebBrowser和一个按钮,在Form1.cs中粘贴如下代码:
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;
using System.Reflection;
using OFFICECORE = Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using System.Runtime.InteropServices;
namespace pptTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//文件加载完调用播放下一页
this.axWebBrowser1.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(axWebBrowser1_DocumentComplete);
this.axWebBrowser1.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2);
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);
}
private Object oDocument;
private String strFileName;
private int totalPage=0;
private int nowlPage = 0;
PowerPoint.Presentation objPresSet = null;
PowerPoint.Application pptApplication;
private void button1_Click(object sender, EventArgs e)
{
//Find the Office document.
openFileDialog1.FileName = "";
openFileDialog1.ShowDialog();
strFileName = openFileDialog1.FileName;
//打开文档
if (strFileName.Length != 0)
{
Object refmissing = System.Reflection.Missing.Value;
oDocument = null;
axWebBrowser1.Navigate(strFileName, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
}
}
public void Form1_Load(object sender, System.EventArgs e)
{
openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt, *.pptx, *.mp3,*.wmv)|*.doc;*.xls;*.ppt;*.pptx;*.mp3;*.wmv";
openFileDialog1.FilterIndex = 1;
}
public void Form1_Closed(object sender, System.EventArgs e)
{
oDocument = null;
}
public void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
Object o = e.pDisp;
oDocument = o.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, o, null);
Object oApplication = o.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, oDocument, null);
pptApplication = (PowerPoint.Application)oApplication;
//取出ppt对象进行设置
pptApplication.SlideShowEnd+=new PowerPoint.EApplication_SlideShowEndEventHandler(pptApplication_SlideShowEnd);
objPresSet = pptApplication.Presentations.Open(strFileName, OFFICECORE.MsoTriState.msoCTrue, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);
int Slides = objPresSet.Slides.Count;
totalPage = Slides;
pptApplication.SlideShowNextSlide += new Microsoft.Office.Interop.PowerPoint.EApplication_SlideShowNextSlideEventHandler(pptApplication_SlideShowNextSlide);
}
//播放下一页处理
void pptApplication_SlideShowNextSlide(Microsoft.Office.Interop.PowerPoint.SlideShowWindow Wn)
{
System.Threading.Thread.Sleep(5000);
NextSlide();
}
//结束事件处理
void pptApplication_SlideShowEnd(Microsoft.Office.Interop.PowerPoint.Presentation Pres)
{
//MessageBox.Show("end");
}
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
public void NextSlide()
{
//if (this.pptApplication != null)
//this.objSST.EntryEffect = PowerPoint.PpEntryEffect.ppEffectCircleOut;
nowlPage++;
if (nowlPage != totalPage)
{
this.objPresSet.SlideShowWindow.View.Next();
}
else {
//如果播放完成,ppt退出axWebBrowser1
//this.objPresSet.SlideShowWindow.View.Exit();
totalPage = 0;
nowlPage = 0;
objPresSet.Close();
int lpdwProcessId;
GetWindowThreadProcessId(new IntPtr(pptApplication.HWND), out lpdwProcessId);
System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
}
}
//加载完后让播放下一页
public void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
System.Threading.Thread.Sleep(5000);
NextSlide();
}
}
}
3.运行播放,可以看到自动播放效果。