Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

c# - How can I embed a PowerPoint presentation into a WPF application without opening another window?

Currently I have a WPF application in C#, but I'm finding it to be incredibly difficult to find any useful ways to embed a PowerPoint presentation into my window.

One solution I found here: Embedding a Powerpoint show into a C# application

This solution created the problem of having PowerPoint run in another window, but just display its UI within the WPF application. This meant that when the WPF window was focused, the PowerPoint presentation was not, and stopped playing. There was also the problem of PowerPoint crashing when the window was closed.

Another solution I found was here: http://www.codeproject.com/Articles/118676/Embedding-PowerPoint-presentation-player-into-a-WP

The solution was popular, but I found it difficult to work with. I don't know any Win32 programming, OR C++, so I found it extremely hard to modify. I managed to get it to stop displaying a second copy of the PowerPoint (an intended function in the original project), but I've not yet found a way to automatically open the PowerPoint presentation.

So what I need is a way to cleanly open the PowerPoint presentation automatically and in the background (I don't want the PowerPoint UI to be displayed at any point), and to allow it to run automatically (and not respond to input) while the application is running. It would be wonderful if I could keep it within C# and WPF, and not have to deal with Win32 and C++.

Is this possible? At this point I'm really regretting this project simply because of the PowerPoint integration headaches.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could convert your presentation to a video format on-the-fly:

// 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;
}

And then you would play it with MediaElement:

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

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

Don't forget to File.Delete(wmvfile) when you're done playing the video!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...