如何在PowerPoint幻灯片放映期间以编程方式调用一个操作?

16

我正在使用Coded UI和VSTO自动化PowerPoint场景。在我的PowerPoint演示文稿中,我已经创建了一个“操作”设置,在形状上启动记事本。在幻灯片放映期间,我需要通过单击“文本/形状”来调用此操作,以便它将打开notepad.exe。有谁能帮我实现这个功能?我编写了以下代码。

//To launch Powepoint
PowerPoint.Application objPPT = new PowerPoint.Application();
objPPT.Visible = Office.MsoTriState.msoTrue;

//Add new presentation
PowerPoint.Presentations oPresSet = objPPT.Presentations;
PowerPoint.Presentation oPres = oPresSet.Add(Office.MsoTriState.msoTrue);

//Add a slide
 PowerPoint.Slides oSlides = oPres.Slides;
PowerPoint.Slide oSlide = oSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);

//Add text
 PowerPoint.TextRange tr = oSlide.Shapes[1].TextFrame.TextRange;
tr.Text = "Launch notepad";
tr.Select();

//Add Action settings on the shape
oSlide.Shapes[1].ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionRunProgram;
oSlide.Shapes[1].ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = "c:\\windows\\notepad.exe";

//start slideshow
objPPT.ActivePresentation.SlideShowSettings.Run();

这将启动演示文稿的幻灯片,并显示第一张幻灯片“在形状上定义操作设置”。现在我该如何通过API自动启动notepad.exe?不幸的是,编码UI无法检测幻灯片中的对象。因此,UI鼠标单击选项可能不可行。

[编辑] 已经取得了在幻灯片放映期间获取形状对象的一点进展。这是以上代码的扩展部分。

PowerPoint.SlideShowWindow oSsWnd = objPPT.ActivePresentation.SlideShowWindow;
PowerPoint.Shape oShape = oSsWnd.View.Slide.Shapes[1];

我不清楚你想要实现什么。如果你想要创建一个演示文稿,那么为什么要通过PowerPoint来启动它并打开记事本呢?让你的代码在创建和启动PPT演示后启动记事本即可。 - Steve Rindsberg
这是一个自动化场景,用于验证操作是否正常工作。因此我只能按照这种方式进行。 - satya
我明白了。我不知道有什么方法可以自动点击屏幕上的任何特定形状或点。 - Steve Rindsberg
如果您只想测试运行命令是否正确,您可以枚举这些形状并使用 Shell(ActionSettings[Click].Run),如果 Action = ppActionRunProgram。 - Paul B.
Paul B,我无法弄清楚如何在这里使用Shell函数。你能否进一步详细说明一下? - satya
2个回答

7
不要问我为什么C#会表现出这样的行为,但它确实这样!你必须执行两次命令才能使其起作用...
    private void button1_Click(object sender, EventArgs e)
    {
        //To launch Powepoint
        PowerPoint.Application objPPT = new PowerPoint.Application();
        objPPT.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;

        //Add new presentation
        PowerPoint.Presentations oPresSet = objPPT.Presentations;
        PowerPoint.Presentation oPres = oPresSet.Add(Microsoft.Office.Core.MsoTriState.msoTrue);

        //Add a slide
        PowerPoint.Slides oSlides = oPres.Slides;
        PowerPoint.Slide oSlide = oSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);

        //Add text
        PowerPoint.TextRange tr = oSlide.Shapes[1].TextFrame.TextRange;
        tr.Text = "Launch notepad";
        //tr.Select();

        //Add Action settings on the shape
        oSlide.Shapes[1].ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionRunProgram;
        oSlide.Shapes[1].ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = @"C:\WINDOWS\system32\notepad.exe";
        oSlide.Shapes[1].ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionRunProgram;
        oSlide.Shapes[1].ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = @"C:\WINDOWS\system32\notepad.exe";
        //start slideshow
        objPPT.ActivePresentation.SlideShowSettings.Run();

    }

抱歉,Siddhart。我想知道的是如何在演示文稿幻灯片播放期间“调用”操作。你的代码只有如何在形状上设置动作设置,这一点我已经知道了。希望你能理解这个区别。我仍在寻找答案。 - satya

5
这可能比您期望的更为复杂,但如果您能够以某种方式确定“文本/形状”对象在屏幕上的X和Y坐标(也许使用Coded UI和VSTO库?),则可以使用User32“SendInput”方法模拟将鼠标移动到对象位置,然后模拟鼠标单击。
以下是模拟用户输入的代码:
int x, y;
// ...  First obtain the X and Y coordinate of the "text/shape" object from APIs

//
InputEmulator inputEmulator = new InputEmulator();
inputEmulator.MoveMouse(x, y);
inputEmulator.ClickMouse();

以下是我使用的InputEmulator类的简化版本,用于模拟Windows UI操作:

class InputEmulator
{
    private const int INPUT_MOUSE = 0;
    private const uint MOUSEEVENTF_MOVE = 0x0001;
    private const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
    private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const uint MOUSEEVENTF_LEFTUP = 0x0004;

    public void MoveMouse(int x, int y)
    {
        INPUT[] inp = new INPUT[1];
        inp[0].type = INPUT_MOUSE;
        inp[0].mi = createMouseInput(x, y, 0, 0, MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE);


        SendInput((uint)1, inp, Marshal.SizeOf(inp[0].GetType()));
    }

    public void ClickMouse()
    {
        INPUT[] inp = new INPUT[2];
        inp[0].type = INPUT_MOUSE;
        inp[0].mi = createMouseInput(0, 0, 0, 0, MOUSEEVENTF_LEFTDOWN);
        inp[1].type = INPUT_MOUSE;
        inp[1].mi = createMouseInput(0, 0, 0, 0, MOUSEEVENTF_LEFTUP);
        SendInput((uint)inp.Length, inp, Marshal.SizeOf(inp[0].GetType()));
    }

    [DllImport("user32.dll", SetLastError = true)]
    private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

    private static MOUSEINPUT createMouseInput(int x, int y, uint data, uint t, uint flag)
    {
        MOUSEINPUT mi = new MOUSEINPUT();
        mi.dx = x;
        mi.dy = y;
        mi.mouseData = data;
        mi.time = t;
        //mi.dwFlags = MOUSEEVENTF_ABSOLUTE| MOUSEEVENTF_MOVE;
        mi.dwFlags = flag;
        return mi;
    }

    [StructLayout(LayoutKind.Explicit)]
    private struct INPUT
    {
        [FieldOffset(0)]
        public int type;
        [FieldOffset(sizeof(int))] //[FieldOffset(8)] for x64
        public MOUSEINPUT mi;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct MOUSEINPUT
    {
        public int dx;
        public int dy;
        public uint mouseData;
        public uint dwFlags;
        public uint time;
        public IntPtr dwExtraInfo;
    }
}

谢谢nomizzz。看起来很有前途。一旦我寻找基于SDK的解决方案失败,我将尝试实现它。 - satya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接