简体   繁体   English

当我在 c#.net 中的表单上显示 Powerpoint 时出现问题

[英]Issue when I display Powerpoint on my form in c#.net

Actullay I want powerpoint must be open and display on my Form itself.and I don't want to open powerpoint in microsoft...so I have done what I want,means powerpoint is opening in my Own form itself. Actullay 我希望powerpoint 必须打开并显示在我的表单上。我不想在microsoft 中打开powerpoint ......所以我做了我想做的,意味着powerpoint 正在我自己的表单中打开。 But issue is that at runtime powerpoint open on form, at the same time one other instance of powerpoint is opening and I don't want that other one.但问题是,在运行时在表单上打开 powerpoint,同时打开另一个 powerpoint 实例,我不想要另一个实例。 So how to remove that other instance of powerpoint?那么如何删除powerpoint的另一个实例呢? please check code.请检查代码。

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

  public partial class Form2 : Form
  {
  [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    static extern IntPtr FindWindow(IntPtr ZeroOnly, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool SetWindowText(IntPtr hwnd, String lpString);

    ppt.Presentation presentation;
    Microsoft.Office.Interop.PowerPoint.SlideShowView oSlideShowView;
    bool flag = false;
    public Form2()
    {
        InitializeComponent();
    }


    public void open(string FileName)
    {
        try
        {
            ppt.Application application;

            // For Display in Panel
            IntPtr screenClasshWnd = (IntPtr)0;
            IntPtr x = (IntPtr)0;

            application = new ppt.Application();

            presentation = application.Presentations.Open(FileName, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

            panel1.Controls.Add(application as Control);
            ppt.SlideShowSettings sst1 = presentation.SlideShowSettings;

            sst1.LoopUntilStopped = Microsoft.Office.Core.MsoTriState.msoTrue;

            ppt.Slides objSlides = presentation.Slides;

            sst1.LoopUntilStopped = MsoTriState.msoTrue;

            sst1.StartingSlide = 1;
            sst1.EndingSlide = objSlides.Count;

            panel1.Dock = DockStyle.Fill;

            sst1.ShowType = ppt.PpSlideShowType.ppShowTypeKiosk;

            ppt.SlideShowWindow sw = sst1.Run();
            //sw=Objssws
            oSlideShowView = presentation.SlideShowWindow.View;

            IntPtr pptptr = (IntPtr)sw.HWND;
            SetParent(pptptr, panel1.Handle);


        }
        catch (Exception)
        {

            throw;
        }

    }

    private void Form2_Load(object sender, EventArgs e)
    {
         OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
       // openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
        if (openFileDialog.ShowDialog(this) == DialogResult.OK)
        //string FileName = "C:\\Task\\Welcome to PowerPoint.ppt";
        open(openFileDialog.FileName);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
    }

    private void button1_Click(object sender, EventArgs e)
    {
        oSlideShowView.Next();
    }
}

尝试设置application.Visible = false

Simply replace the following line只需替换以下行

presentation = application.Presentations.Open(FileName, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);演示文稿 = application.Presentations.Open(FileName, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

with the following code line: presentation = application.Presentations.Open(FileName, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);使用以下代码行: presentation = application.Presentations.Open(FileName, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM