c#开发PPT多屏显示并控制

使用C#开发,应用程序winform或者wpf在多个扩展屏上控制PPT
目前使用Microsoft.Office.Interop.PowerPoint已实现控制PPT首页、未页、上下页、跳转指定页等基础功能。
场景是一台电脑两个外接显示器,打开PPT文档,在电脑上播放,电脑显示第一页,软件上控制下一页按钮,下一页幻灯片到第一个外接显示器,以此类推,还有三个屏幕显示同一页幻灯片。
多屏实例多个Presentation对象情况下可以实现,但是会出现打开了多个PPT,在打开一个PPT的情况下如何实现多屏控制并显示PPT内容?


private void NextSlideButton_Click(object sender, EventArgs e)
{
    currentSlide++;
    if (currentSlide >= pptDocument.Slides.Count)
    {
        currentSlide = 0;
    }
    Rectangle screenBounds = new Rectangle(displays[0].WorkingArea.Left, displays[0].WorkingArea.Top, displays[0].WorkingArea.Width, displays[0].WorkingArea.Height);
    int slideWidth = (int)(screenBounds.Width * 0.8); // 每张幻灯片宽度为屏幕宽度的80%
    int slideHeight = (int)(screenBounds.Height * 0.8); // 每张幻灯片高度为屏幕高度的80%
    int slideX = screenBounds.Left; // 从屏幕左边界开始显示第一张幻灯片
    int slideY = screenBounds.Top; // 从屏幕顶部开始显示第一张幻灯片
    Bitmap bitmap = pptDocument.Slides[currentSlide].Export(ExportFormat.Png, slideX, slideY, slideWidth, slideHeight);
    using (Graphics graphics = Graphics.FromImage(bitmap))
    {
        graphics.DrawImage(bitmap, new Point(slideX, slideY));
    }
}

在每个PPT窗口中打开PPT文件

foreach(var window in pptWindows)
{
    var ppt = new Microsoft.Office.Interop.PowerPoint.Application();
    var presentation = ppt.Presentations.Open("your-ppt-file-path", MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
    presentation.SlideShowSettings.ShowPresenterView = MsoTriState.msoTrue;
    presentation.SlideShowSettings.Run();
    window.Controls.Add(new AxHost());
    window.Controls[0].CreateControl();
    window.Controls[0].Width = window.Width;
    window.Controls[0].Height = window.Height;
    var hwnd = presentation.SlideShowWindow.HWND;
    SetParent(hwnd, window.Controls[0].Handle);
}

获取显示器信息和计算PPT显示区域后,可以控制PPT翻页,并在多个显示器同时显示同一PPT内容,实现跨屏幕控制PPT

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt:

    首先,根据参考资料中的段落0,你需要自己百度配置DCOM组件的权限方法,因为缺乏具体的配置步骤。

    然后,根据参考资料中的段落1,你可以按照以下步骤引入所需的库文件:

    1. 打开Visual Studio,创建一个新的C#项目。
    2. 右击项目,选择"管理NuGet程序包"。
    3. 在NuGet包管理器中搜索"Microsoft.Office.Interop.PowerPoint"。
    4. 选择所需的版本,并点击"安装"进行安装。

    接下来,根据参考资料中的段落2,你可以按照以下步骤创建PPT中的幻灯片:

    1. 使用Presentation对象打开PPT文档。
    2. 使用Slides.Add方法添加幻灯片,指定要添加的页码。

    示例代码如下:

    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    
    // 打开PPT文档
    PowerPoint.Application pptApplication = new PowerPoint.Application();
    PowerPoint.Presentation presentation = pptApplication.Presentations.Open("path_to_ppt_file");
    
    // 创建幻灯片
    PowerPoint.Slide slide = presentation.Slides.Add(page, PowerPoint.PpSlideLayout.ppLayoutBlank);
    

    接下来,根据参考资料中的段落4,你可以按照以下步骤创建表格:

    1. 使用Shapes.AddTable方法添加表格,并指定行数和列数。
    2. 使用循环为每个单元格设置属性,如行高、列宽、字体大小、字体颜色等。

    示例代码如下:

    PowerPoint.Table table = slide.Shapes.AddTable(rowCount, colCount, 10, 105, 900, 300).Table;
    for (int row = 1; row <= table.Rows.Count; row++)
    {
        table.Rows[row].Height = 60; // 设置行高
    
        for (int cell = 1; cell <= table.Columns.Count; cell++)
        { 
            table.Columns[cell].Width = 50; // 设置列宽
    
            // 设置字体属性等
            table.Cell(row, cell).Shape.TextFrame.TextRange.Font.Size = 20;
            table.Cell(row, cell).Shape.TextFrame.TextRange.Font.Color.RGB = 0 + 0 * 256 + 0 * 256 * 256;
            // 其他设置...
    
            table.Cell(row, cell).Shape.TextFrame.TextRange.Text = "AAA"; // 单元格赋值
        }
    }
    

    最后,根据参考资料中的段落3,你可以使用以下步骤保存和关闭PPT:

    1. 使用SaveAs方法将PPT保存到指定路径。
    2. 使用Close方法关闭PPT。
    3. 使用Quit方法关闭PPT应用程序池。
    4. 使用GC.Collect进行垃圾回收。

    示例代码如下:

    string savePath = "path_to_save_ppt_file";
    
    // 保存PPT到指定路径
    presentation.SaveAs(savePath, PowerPoint.PpSaveAsFileType.ppSaveAsDefault, Microsoft.Office.Core.MsoTriState.msoFalse);
    // 关闭PPT
    presentation.Close();
    // 关闭PPT应用程序池
    pptApplication.Quit();
    // 垃圾回收
    GC.Collect();
    

    这些步骤可以帮助你使用C#开发在多个扩展屏上控制并显示PPT内容。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

多屏显示的C#代码控制
win7以上的系统在分辨率设置里有这么一个选项,可以控制多屏显示的时候是复制屏幕还是拓展屏幕。
我只搞懂了一个方法,就是【SetScreen】,功能是设置多屏幕的显示方式。
参数是uint类型,2应该是复制屏幕,4应该是拓展屏幕。

        /// <summary>
        /// 分屏表示
        /// </summary>
        /// <param name="i"></param>
        public Form(int i = 0)
        {
            InitializeComponent();
 
            bmp1 = new Bitmap(@"C:\Users\Desktop\185.jpg");
            bmp2 = new Bitmap(@"C:\Users\Desktop\aaa.jpg");
 
            if (i == 0)
            {
                this.Text = "Form1";
                this.pictureBox1.Image = bmp1;
 
                for (int j = 1; j < Screen.AllScreens.Length; j++)
                {
                    Form form2 = new Form(j);
                    form2.Text = "Form" + (j + 1);
                    form2.pictureBox1.Image = bmp2;
                    form2.Show();
                }
            }
            index = i;
        }
 
 
        /// <summary>
        /// 把窗口移动到其他屏幕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_Shown(object sender, EventArgs e)
        {
            // 分屏显示
            Screen[] sc = Screen.AllScreens;
            if (index >= sc.Length)
            {
                index = 0;
            }
 
            this.Location = new Point(sc[index].Bounds.Left, 0);
            this.WindowState = FormWindowState.Maximized;
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            for (int j = 0; j < Application.OpenForms.Count; j++)
            {
                Form form = Application.OpenForms[j];
                ...........
            }
        }

可以通过设置每个显示器的分辨率和缩放比例,使每个显示器显示同一页幻灯片。可以使用System.Windows.Forms.Screen类的Bounds属性和WorkArea属性来获取和设置显示器的位置和大小。
可以参考:https://www.wenmi.com/article/pyw1w3032we0.html

试试Microsoft.Office.Interop.PowerPoint库中提供的方法来控制和显示PPT

引用chatgpt内容作答:
要实现多屏控制并显示PPT内容,您可以考虑以下步骤:

1、确定屏幕布局:首先,您需要确定每个外接显示器在系统中的位置和尺寸。您可以使用System.Windows.Forms.Screen.AllScreens属性获取所有连接的屏幕,并使用Screen对象的属性(如Bounds和WorkingArea)来获取屏幕的位置和大小信息。

2、创建窗口:在您的应用程序中,创建一个主窗口,用于控制PPT的显示。您可以使用Windows Forms或WPF来创建窗口,具体取决于您的偏好和项目要求。

3、控制PPT播放:使用Microsoft.Office.Interop.PowerPoint库中的对象来打开和控制PPT文档。您已经实现了基础功能,如首页、未页、上下页和跳转指定页等。您可以继续使用这些功能,并根据用户的输入来控制PPT的播放。

4、多屏显示:为了在多个屏幕上显示PPT内容,您可以使用多个窗口或自定义控件来实现。在主窗口中,您可以创建多个子窗口或控件,每个子窗口或控件对应一个外接显示器。您可以根据每个屏幕的位置和大小来设置子窗口或控件的位置和大小,确保它们在正确的位置上显示。

5、导出动画效果:如果您需要在外接显示器上显示PPT的动画效果,可以考虑将每个幻灯片导出为图片序列,并在外接显示器上以适当的速度播放这些图片。您可以使用Slide.Export方法将每个幻灯片导出为图片,并使用适当的延迟在外接显示器上显示这些图片。

需要注意的是,这只是一种基本的实现思路,您可能需要根据具体的项目需求进行适当的调整和扩展。另外,使用Microsoft.Office.Interop.PowerPoint库进行PPT控制时,您需要确保目标计算机上安装了Microsoft Office,并且具有足够的权限来与PPT交互。
以下是一个示例代码,用于在多个扩展屏上控制并显示PPT内容:

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Office.Interop.PowerPoint;

namespace PPTController
{
    public partial class MainForm : Form
    {
        private Application pptApplication;
        private Presentation pptPresentation;
        private Slides pptSlides;
        private int currentSlideIndex;

        private Screen[] screens;
        private Form[] slideForms;

        public MainForm()
        {
            InitializeComponent();
            InitializePowerPoint();
            InitializeScreens();
            InitializeSlideForms();
        }

        private void InitializePowerPoint()
        {
            pptApplication = new Application();
            pptPresentation = pptApplication.Presentations.Open("path_to_ppt_file");
            pptSlides = pptPresentation.Slides;
            currentSlideIndex = 1;
        }

        private void InitializeScreens()
        {
            screens = Screen.AllScreens;
        }

        private void InitializeSlideForms()
        {
            slideForms = new Form[screens.Length];

            for (int i = 0; i < screens.Length; i++)
            {
                slideForms[i] = new Form();
                slideForms[i].FormBorderStyle = FormBorderStyle.None;
                slideForms[i].WindowState = FormWindowState.Maximized;
                slideForms[i].StartPosition = FormStartPosition.Manual;
                slideForms[i].Location = screens[i].Bounds.Location;
                slideForms[i].Size = screens[i].Bounds.Size;

                PictureBox pictureBox = new PictureBox();
                pictureBox.Dock = DockStyle.Fill;
                slideForms[i].Controls.Add(pictureBox);
            }
        }

        private void DisplaySlide(int slideIndex)
        {
            for (int i = 0; i < screens.Length; i++)
            {
                Slide slide = pptSlides[slideIndex];
                string imagePath = $"slide_{slideIndex}.png";
                slide.Export(imagePath, "PNG", 1024, 768); // 导出当前幻灯片为图片

                PictureBox pictureBox = (PictureBox)slideForms[i].Controls[0];
                pictureBox.Image = Image.FromFile(imagePath);
                slideForms[i].Show();
            }
        }

        private void HideSlideForms()
        {
            foreach (Form slideForm in slideForms)
            {
                slideForm.Hide();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            currentSlideIndex++;
            if (currentSlideIndex > pptSlides.Count)
                currentSlideIndex = pptSlides.Count;

            HideSlideForms();
            DisplaySlide(currentSlideIndex);
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            currentSlideIndex--;
            if (currentSlideIndex < 1)
                currentSlideIndex = 1;

            HideSlideForms();
            DisplaySlide(currentSlideIndex);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            pptPresentation.Close();
            pptApplication.Quit();

            foreach (Form slideForm in slideForms)
            {
                slideForm.Close();
            }
        }
    }
}

这个示例代码假设您已经创建了一个Windows Forms应用程序,并在窗体上放置了两个按钮("btnNext"和"btnPrevious"),用于控制PPT的下一页和上一页功能。在窗体的FormClosing事件中,关闭PPT和所有的幻灯片窗口。

这只是一个基本示例,您可能需要根据实际需求进行适当的修改和扩展。还要确保将"path_to_ppt_file"替换为您实际的PPT文件路径。

希望这可以帮助到您!

回答部分参考、引用ChatGpt以便为您提供更准确的答案:

要实现C#开发中的多屏控制和显示PPT内容,可以使用以下步骤:

  1. 获取系统上所有的显示器: 在C#中,可以使用Screen.AllScreens属性获取当前系统上所有的显示器。这将返回一个Screen数组,其中包含每个显示器的详细信息。
  2. 创建PPT对象并打开文档: 使用Microsoft.Office.Interop.PowerPoint命名空间中的类,创建一个PowerPoint应用程序对象,然后使用Application.Presentations.Open方法打开PPT文档。
  3. 设置显示器和窗口位置: 可以使用Application.ActivePresentation.SlideShowSettings.Run()方法启动幻灯片播放,并通过设置SlideShowWindow.LeftSlideShowWindow.Top属性来控制PPT窗口在不同显示器上的位置。
  4. 控制幻灯片显示: 通过使用SlideShowView对象,可以控制PPT的幻灯片显示。例如,使用SlideShowView.Next方法来切换到下一页,使用SlideShowView.Previous方法来切换到上一页,使用SlideShowView.GotoSlide方法来跳转到指定页等。
  5. 多屏显示PPT内容: 首先,在应用程序的主屏幕上显示第一页幻灯片。然后,使用SlideShowWindow对象的Activate方法将PPT窗口切换到其他外接显示器上,通过设置SlideShowWindow.LeftSlideShowWindow.Top属性,将PPT窗口移动到相应的显示器上。根据您的需求,可以使用循环将同一页幻灯片显示在其他的三个屏幕上。

需要注意的是,确保正确处理异常情况,例如检查显示器数量和索引的有效性,以及在程序关闭时正确释放PPT对象等。

这些步骤可以作为一个基本框架,您可以根据具体的需求进行修改和扩展。请注意,使用Microsoft.Office.Interop.PowerPoint库需要在开发环境中安装相应的Microsoft Office组件。

要实现多屏控制并显示PPT内容,你可以使用C#的WinForms或WPF应用程序结合Windows API来实现。以下是一种可能的方法:

  1. 获取所有连接的显示器信息:使用System.Windows.Forms.Screen.AllScreens属性获取所有连接的显示器的信息。这将返回一个Screen数组,其中包含每个显示器的详细信息,如位置、大小等。

  2. 创建窗口并设置位置:根据需要在每个显示器上创建一个窗口,并将其位置设置为对应的显示器。可以使用System.Windows.Forms.Form类(WinForms)或System.Windows.Window类(WPF)来创建窗口,并使用窗口的Location属性设置其位置。

  3. 控制PPT播放:使用Microsoft.Office.Interop.PowerPoint库中的相关功能来控制PPT的播放,如打开PPT文档、跳转到指定页面等。你已经提到了Microsoft.Office.Interop.PowerPoint库,可以继续使用它来实现PPT的控制。

  4. 同步多屏显示:根据当前所处的页面,将对应的PPT内容显示在相应的窗口上。你可以使用Microsoft.Office.Interop.PowerPoint库中的相关功能来获取当前页的内容,并将其显示在对应的窗口上。

  5. 监听控制事件:在你的应用程序中添加相应的控件或按钮来监听用户的控制事件,如下一页、上一页等。当用户触发这些事件时,根据当前所处的页面更新PPT内容,并将其显示在相应的窗口上。

请注意,这只是一种概念性的方法,你需要根据具体的需求和环境来进行进一步的实现。同时,你可能需要处理多线程、异常处理等方面的问题,以确保应用程序的稳定性和流畅性。

另外,也可以考虑使用专门的第三方库或工具来简化多屏控制和显示PPT内容的过程。例如,Syncfusion、DevExpress和Telerik等公司提供了各种用于多屏显示和控制的UI组件和工具。

在打开一个PPT的情况下实现多屏控制并显示PPT内容,您可以使用以下步骤:

  1. 使用C#开发的WinForms或WPF应用程序,确保已引用Microsoft.Office.Interop.PowerPoint库。
  2. 在应用程序中,创建一个Presentation对象,打开PPT文档。
  3. 获取系统上连接的所有显示器的信息,可以使用System.Windows.Forms.Screen.AllScreens属性。
  4. 根据连接的显示器数量,创建对应数量的PowerPoint对象,每个对象代表一个显示器。
  5. 使用Presentation对象的Copy方法,将当前幻灯片复制到每个PowerPoint对象中。
  6. 使用PowerPoint对象的SlideShowSettings对象,设置每个对象的幻灯片放映设置。例如,设置显示器的范围和位置。
  7. 启动每个PowerPoint对象的幻灯片放映,以便在相应的显示器上显示幻灯片。
    这样,就可以在一个PPT文档的情况下,通过多个PowerPoint对象实现多屏控制并显示PPT内容。