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

后端 未结 4 700
攒了一身酷
攒了一身酷 2020-12-16 02:30

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.

O

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 03:28

    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:

    
    
    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!

提交回复
热议问题