将powerpoint幻灯片嵌入控制器

时间:2012-12-04 05:29:56

标签: c# wpf powerpoint office-interop setparent

我有一个将powepoint文件嵌入webBrowser控件的wpf应用程序。我设法使用下面的示例代码实现该功能。但每当我运行ppt文件时,幻灯片放映会弹出屏幕几秒钟,然后只会嵌入到控件中。有没有办法阻止它或将其作为后台进程运行?

using System;
using System.Runtime.InteropServices;
using System.Windows;
using Microsoft.Office.Core;
using PPT = Microsoft.Office.Interop.PowerPoint;

namespace WpfApplication1
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        [DllImport("User32", CharSet = CharSet.Auto, ExactSpelling = true)]
        internal static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndParent);

        private void BtnOpenClick(object sender, RoutedEventArgs e)
        {
            const string pptFilePath = @"E:\Sample.ppt";
            var pptApplication = new PPT.Application
            {
                ShowWindowsInTaskbar = MsoTriState.msoFalse 
            };            

            var presentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoCTrue,
                                                                          MsoTriState.msoTrue, MsoTriState.msoFalse);

            var slideShowWindow = presentation.SlideShowSettings.Run();

            slideShowWindow.Height = (int)(0.85 * webBrowser1.Height);
            slideShowWindow.Width = (int)(0.75 * webBrowser1.Width);           

            SetParent(new IntPtr(presentation.SlideShowWindow.HWND), webBrowser1.Handle);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我使用了Patrick Reisert的solution。他使用powerpointviewer而不是powerpoint,并将幻灯片转换为位图列表。它是一个具有导航功能的综合解决方案。

答案 1 :(得分:-1)

你也可以用我已实现的另一种方式打开ppt / pps。 它会在全屏幕上直接打开您的ppt / pps文件。如果你想这样做,试试这个。

 Microsoft.Office.Interop.PowerPoint.Application application = new Microsoft.Office.Interop.PowerPoint.Application();
                Microsoft.Office.Interop.PowerPoint.Presentation presesntation = application.Presentations.Open2007("file path", Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue);
                Microsoft.Office.Interop.PowerPoint.SlideShowSettings sst = presesntation.SlideShowSettings;
                sst.ShowType = Microsoft.Office.Interop.PowerPoint.PpSlideShowType.ppShowTypeSpeaker;
                sst.Run();