c#桌面winform通过wps调用power point文件播放,播放完立刻退出不留痕迹

c#桌面winform通过wps调用power point文件播放,播放完立刻退出,不留痕迹.

以前一直用微软的office那个interop,愁死了,版本动不动就不对,加上国产化要求,干脆研究了一下换成wps office。原创,转载请注明海宏软件。

先安装wps office。

新建一个winform程序,

添加wps引用:

 

添加代码:

private void PlayPowerPoint(string filePath)
{
    try
    {
        // 创建 PowerPoint 应用实例
        PowerPoint.Application pptApp = new PowerPoint.Application();
        pptApp.Visible = PowerPoint.MsoTriState.msoTrue;// 使 PowerPoint 可见(可选)
        //

        // 打开演示文稿
        PowerPoint.Presentations presentations = pptApp.Presentations;
        PowerPoint.Presentation pptPres = presentations.Open(filePath, WithWindow: PowerPoint.MsoTriState.msoTrue);
        //// 开始播放演示文稿
        //pptPres.SlideShowSettings.Run();

        //// 注意:如果要让窗体保持活动状态,可以调用 Application.Activate() 或其他方式保持窗口在前台。
        //// pptApp.Activate(); // 使 PowerPoint 窗口在前台(在某些情况下可能需要)

        // 获取幻灯片放映配置
        PowerPoint.SlideShowSettings showSettings = pptPres.SlideShowSettings;
        showSettings.LoopUntilStopped =PowerPoint.MsoTriState.msoFalse;  // 禁用循环播放  
        showSettings.StartingSlide = 1;                       // 从第一页开始  
        showSettings.EndingSlide = pptPres.Slides.Count;      // 到最后一页结束  

        // 启动放映并获取窗口对象  
        PowerPoint.SlideShowWindow showWindow = showSettings.Run();

        // 定义事件处理程序  
        pptApp.SlideShowEnd += (Pres) =>
        {
            // 关闭 PPT 并释放资源
            try
            {
                Pres.Close();
                pptApp.Quit();
            }
            catch (Exception x)
            {

            }
            Marshal.ReleaseComObject(pptPres);
            Marshal.ReleaseComObject(pptApp);
        };
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error opening PowerPoint presentation: " + ex.Message);
    }
}

 

按钮引用:

private void button2_Click(object sender, EventArgs e)
{
    PlayPowerPoint("d:\\test.pptx");
}

效果杠杠的!

 

附录用网页版打开office文件的方法,例子如下:

https://view.officeapps.live.com/op/embed.aspx?src=http%3A%2F%2Fsaas.ihaihong.cn%2Fliaoyang%2Ftemp%2Ftest.pptx

 

posted @ 2025-03-04 18:35  海宏软件  阅读(224)  评论(0)    收藏  举报