gpt4 book ai didi

c# - 如何在不打开另一个窗口的情况下将 PowerPoint 演示文稿嵌入到 WPF 应用程序中?

转载 作者:太空狗 更新时间:2023-10-29 17:40:23 24 4
gpt4 key购买 nike

目前我有一个用 C# 编写的 WPF 应用程序,但我发现很难找到任何有用的方法将 PowerPoint 演示文稿嵌入到我的窗口中。

我在这里找到了一个解决方案:Embedding a Powerpoint show into a C# application

此解决方案产生了让 PowerPoint 在另一个窗口中运行但仅在 WPF 应用程序中显示其 UI 的问题。这意味着当 WPF 窗口获得焦点时,PowerPoint 演示文稿没有,并停止播放。还有关闭窗口时PowerPoint崩溃的问题。

我在这里找到了另一个解决方案:http://www.codeproject.com/Articles/118676/Embedding-PowerPoint-presentation-player-into-a-WP

该解决方案很受欢迎,但我发现它很难使用。我不知道任何 Win32 编程或 C++,所以我发现修改起来非常困难。我设法让它停止显示 PowerPoint 的第二个副本(原始项目中的预期功能),但我还没有找到自动打开 PowerPoint 演示文稿的方法。

所以我需要的是一种在后台自动干净地打开 PowerPoint 演示文稿的方法(我不希望 PowerPoint UI 在任何时候都显示),并允许它自动运行(并且不响应输入)在应用程序运行时。如果我可以将它保留在 C# 和 WPF 中,而不必处理 Win32 和 C++,那就太好了。

这可能吗?在这一点上,我真的很后悔这个项目,因为 PowerPoint 集成令人头疼。

最佳答案

您可以即时将演示文稿转换为视频格式:

// not tested as I don't have the Office 2010, but should work
private string GetVideoFromPpt(string filename)
{
var app = new PowerPoint.Application();
var presentation = app.Presentations.Open(filename, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

var wmvfile = Guid.NewGuid().ToString() + ".wmv";
var fullpath = Path.GetTempPath() + filename;

try
{
presentation.CreateVideo(wmvfile);
presentation.SaveCopyAs(fullpath, PowerPoint.PpSaveAsFileType.ppSaveAsWMV, MsoTriState.msoCTrue);
}
catch (COMException ex)
{
wmvfile = null;
}
finally
{
app.Quit();
}

return wmvfile;
}

然后您将使用 MediaElement 播放它:

<MediaElement Name="player" LoadedBehavior="Manual" UnloadedBehavior="Stop" />

public void PlayPresentation(string filename)
{
var wmvfile = GetVideoFromPpt(filename);
player.Source = new Uri(wmvfile);
player.Play();
}

当您播放完视频后,不要忘记 File.Delete(wmvfile)!

关于c# - 如何在不打开另一个窗口的情况下将 PowerPoint 演示文稿嵌入到 WPF 应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15725908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com