2

我在主窗体中嵌入了一个 PowerPoint Viewer 以显示一个 pptx 文件。我的电脑上有 2 台显示器,我正在尝试在我的第二台显示器上运行该应用程序。当我在监视器 1 上运行程序时,一切都按预期工作。但是当我在监视器 2 上运行它时(这意味着我更改代码以在第二台监视器上运行应用程序,而不是运行应用程序然后自己将其移动到第二台监视器),PowerPoint Viewer 在第一台监视器上打开,然后移动到程序正在运行的第二个监视器。我相信这与没有正确设置父对象但不太确定的 PowerPoint 对象有关。这是我的代码:

[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 MainForm()
{
    InitializeComponent();            
    DisplayOnMonitor();
}

private void MainForm_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(
                    Settings.Default.PowerPointPath,
                    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, splitContainerControl.Panel1.Handle);

    splitContainerLeft.Panel1.LostFocus += new EventHandler(OnLostFocus);
    splitContainerControl.PreviewKeyDown +=new PreviewKeyDownEventHandler(OnPreviewKeyDown);
    splitContainerLeft.Panel1.AutoSize = true;
    splitContainerControl.Panel1.Focus();
}

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

    this.FormBorderStyle = FormBorderStyle.None;
    this.Left = sc[1].Bounds.Width;
    this.Top = sc[1].Bounds.Height;
    this.StartPosition = FormStartPosition.Manual;
    this.Location = sc[1].Bounds.Location;
    Point p = new Point(sc[1].Bounds.Location.X, sc[1].Bounds.Location.Y);
    this.Location = p;
    this.WindowState = FormWindowState.Maximized;
}

我知道当调用 sst1.Run() 方法时会出现 PowerPoint Viewer,但我试图在此之前(和之后)使应用程序不可见,但它会引发异常。如果有办法指示 sst1 打开哪个显示器,那么我认为它会解决问题,但到目前为止我还没有运气。

任何帮助表示赞赏!

4

0 回答 0