将Powerpoint节目嵌入到C#应用程序中

Kin*_*nus 5 c# powerpoint winforms

我希望能够将powerpoint演示文稿嵌入到C#表单(WinForms)中.基本上我们有一个52'显示器,我们的想法是在一个角落里我们将有一个PPT循环,然后其他三个角落将显示来自程序本身的信息.

我曾预料到这很简单,但似乎我错了.

我被建议使用WebBrowser控件,但这不起作用,而是将powerpoint文件视为下载,即给我一个"保存,打开"对话框.

有什么建议?

AK

slo*_*oth 8

您可以使用SetParent函数运行PowerPoint,获取Windows句柄并设置新的父窗口.


您所需要的只是PowerPoint窗口的窗口类的名称,但是由于Spy ++,这没什么大不了的.

间谍++


这是在自定义应用程序中运行"内部"的PowerPoint的屏幕截图:

微软幻灯片软件


完整示例(取自此处并为PowerPoint修改):

public partial class Form1 : Form
{
    public Form1()
    {
        this.Size = new System.Drawing.Size(800, 600);
        this.TopMost = true;
        this.Text = "My Application";
        this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
        Func<bool> run = () =>
            Window.Find(hwnd =>
            {
                var cn = Window.GetClassName(hwnd);
                var res = (cn == "PPTFrameClass");
                if (res)
                {
                    this.Controls.Clear();
                    Window.SetParent(hwnd, this.Handle);
                    Window.SetWindowPos(hwnd, new IntPtr(0), -8, -30, this.Width + 10, this.Height + 37, 0x0040);
                }
                return res;
            });

        new Button { Parent = this, Text = "Start" }
            .Click += (s, e) =>
            {
                if (run() == false)
                    MessageBox.Show("Open PowerPoint");
            };
    }
}

public static class Window
{
    public static bool Find(Func<IntPtr, bool> fn)
    {
        return EnumWindows((hwnd, lp) => !fn(hwnd), 0) == 0;
    }
    public static string GetClassName(IntPtr hwnd)
    {
        var sb = new StringBuilder(1024);
        GetClassName(hwnd, sb, sb.Capacity);
        return sb.ToString();
    }
    public static uint GetProcessId(IntPtr hwnd)     // {0:X8}
    {
        uint pid;
        GetWindowThreadProcessId(hwnd, out pid);
        return pid;
    }
    public static string GetText(IntPtr hwnd)
    {
        var sb = new StringBuilder(1024);
        GetWindowText(hwnd, sb, sb.Capacity);
        return sb.ToString();
    }

    delegate bool CallBackPtr(IntPtr hwnd, int lParam);

    [DllImport("user32.dll")]
    static extern int EnumWindows(CallBackPtr callPtr, int lPar);

    [DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

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

    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int W, int H, uint uFlags);
}
Run Code Online (Sandbox Code Playgroud)