2013-04-30 41 views
0

Actullay我想要的powerpoint必须打开并显示在我的窗体本身。我不想在微软打开powerpoint ...所以我做了我想做的事,意味着Powerpoint以我自己的形式开放。但问题在于,在运行时Powerpoint在窗体上打开时,同时另一个Powerpoint实例正在打开,我不想要另一个实例。那么如何去除那个powerpoint的其他实例呢? 请检查验证码。问题当我在我的窗体上显示Powerpoint的c#.net

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(); 
    } 
} 
+1

神圣抽烟的人 - 花一些时间来正确地问一个问题。您如何期待本网站的人们花时间回答您没有花时间提问的问题? – 2013-04-30 12:25:04

+0

事实上,我已经阅读过你的问题两次,但仍然不清楚。 – 2013-04-30 12:44:31

回答

0

尝试设置application.Visible =假

+0

Thanx for reply..but钢铁打开其他实例。 – user2335722 2013-04-30 12:45:34

+0

其非常紧急,请给我建议 – user2335722 2013-04-30 13:00:20

0

我真的不知道,你的解决方案可能是如何工作的,也有在代码中的一些时刻不明(例如,什么是PPT?) 所以我创造了另一个项目,并使用this solutionthis one,写代码如下图所示:

/*using*/ 
    using System.Runtime.InteropServices; 
using System.Runtime; 
using Microsoft; 

    namespace Quest_Powerpoint 
{ 
public partial class Form1 : 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); 

    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, UIntPtr lParam); 

    public const uint WM_SETFOCUS = 0x0007; 
    Microsoft.Office.Interop.PowerPoint.Application application; 
    Microsoft.Office.Interop.PowerPoint.Presentation presentation; 

    public Form1() 
    { 
     InitializeComponent(); 
     DisplayOnMonitor(); 
    } 


    private void Form1_Load(object sender, EventArgs e) 
    { 
     LoadSlideShow(); 
    } 

    private void LoadSlideShow() 
    { 
     IntPtr screenClasshWnd = (IntPtr)0; 
     IntPtr x = (IntPtr)0; 

     application = new Microsoft.Office.Interop.PowerPoint.Application(); 
     presentation = application.Presentations.Open2007(
         @"D:\Presentation1.pptx", 
         Microsoft.Office.Core.MsoTriState.msoTrue, 
         Microsoft.Office.Core.MsoTriState.msoFalse, 
         Microsoft.Office.Core.MsoTriState.msoFalse, 
         Microsoft.Office.Core.MsoTriState.msoFalse); 

     Microsoft.Office.Interop.PowerPoint.SlideShowSettings sst1 = presentation.SlideShowSettings; 
     sst1.LoopUntilStopped = Microsoft.Office.Core.MsoTriState.msoTrue; 
     Microsoft.Office.Interop.PowerPoint.SlideShowWindow sw = sst1.Run(); 

     IntPtr pptptr = (IntPtr)sw.HWND; 
     SetParent(pptptr, splitContainer1.Panel1.Handle); 
    } 

    private void DisplayOnMonitor() 
    { 
     Screen[] sc; 
     sc = Screen.AllScreens; 

     this.FormBorderStyle = FormBorderStyle.Fixed3D; 
     this.StartPosition = FormStartPosition.CenterScreen; 


    } 
    } 
    } 

它的工作原理,我可以切换演示文稿页面。

http://puu.sh/2K4i4.jpg

虽然应用工程,POWERPOINT.exe过程在后台 - 我认为这是没有它是不可能的,因为你使用它的发动机。尽管如此,它并不妨碍加载另一个PP实例,并且只打开窗体的一个窗口。

我希望没有进一步的格式化应用程序需要看它是如何工作的。

+0

真的很感谢你的帮助...但这里也有同样的问题。打开powerpoint的其他实例。 – user2335722 2013-04-30 13:51:33

+0

如果你在后台讲述PowerPoint的过程 - 这是你不能没有的。但在测试应用程序时,我还没有注意到PowerPoint的另一个窗口打开(使用Win7,也许这是答案?)。它只在taskBar中显示,没有别的。另外,如果解决方案的创建非常紧迫,也许将它保留原样是有用的,并且在发现您需要的所有内容时添加一些功能。 – 2013-04-30 13:57:05

+0

是的.Thanx.you确实得到了我的观点。我的问题是,它只显示在任务栏中没有别的。 – user2335722 2013-05-01 11:56:37