C# winform以阅览模式打开PPT,并控制PPT上下页,轮播
[DllImport("user32.dll")]
public extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
/// <summary>
/// 打开ppt文件
/// </summary>
/// <param name="filePath">路径</param>
/// <returns></returns>
public string PPTOpen(string filePath)
{
StringBuilder s = new StringBuilder(512);
//防止连续打开多个PPT程序.
if (this.objApp != null)
{
this.objApp.Quit();
GC.Collect();
}
try
{
objApp = new POWERPOINT.Application();
//以非只读方式打开,方便操作结束后保存.
objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);
//objSlides = objPresSet.Slides;
//int[] SlideIdx = new int[3];
//for (int i = 0; i < 3; i++) SlideIdx[i] = i + 1;
//objSldRng = objSlides.Range(SlideIdx);
//objSST = objSldRng.SlideShowTransition;
//objSST.AdvanceOnTime = MsoTriState.msoTrue;
//objSST.AdvanceTime = 5;
//objSST.EntryEffect = POWERPOINT.PpEntryEffect.ppEffectBoxOut;
//bAssistantOn = objApp.Assistant.On;
objApp.Assistant.On = false;
objSSS = this.objPresSet.SlideShowSettings;
pptCount = objPresSet.Slides.Count;
//objSSS.ShowType = POWERPOINT.PpSlideShowType.ppShowTypeWindow;
objSSS.Run();
IntPtr aa = (IntPtr)objPresSet.SlideShowWindow.HWND;
int j = GetWindowText(aa, s, s.Capacity); //把this.handle换成你需要的句柄
}
catch (Exception ex)
{
this.objApp.Quit();
}
return s.ToString();
}
以阅览模式打开PPT文件,这个嗯简单,通过getwindowtext,获取到打开PPT的句柄,便于之后将句柄在winform的窗体中显示
//将ppt嵌入到你想嵌入的控件中
private void intoWebbrower(string pptTitle)
{
ParenthWnd = new IntPtr(0);
ParenthWnd = FindWindow(null, pptTitle);//123.pptx - WPS 演示
SetParent(ParenthWnd, this.pictureBox1.Handle);
int x = 0;
int y = 0;
int width = this.pictureBox1.Width;
int height = this.pictureBox1.Height-100;
MoveWindow(ParenthWnd, x, y, width, height, true);
}
通过SetParent可以现实前面获取到句柄的PPT嵌入到控件中的
而MoveWindow是控制句柄在控件中显示的位置和大小
对于PPT其他的操作上一页下一页就挺简单的了,可以查阅MSDN的Powerpoint
//上一页
public void preButton()
{
try
{
objPresSet.SlideShowWindow.View.Previous();
}
catch { }
}
//下一页
public void nextButton()
{
try
{
objPresSet.SlideShowWindow.View.Next();
}
catch { }
}
播放PPT还有一个比较麻烦的就是需要及时的释放掉PPT的句柄,否者会一直被占用这
public void PPTExit()
{
try
{
if (objApp != null)
objApp.SlideShowWindows[1].View.Exit();
}
catch { }
}
而轮播的效果也可以通过objApp.SlideShowWindows[1].View.Slide.SlideIndex目前播放的是第几页来实现轮播的效果,
检测到是最后一页是objPresSet.SlideShowWindow.View.First();,就OK了