我正在使用编码的UI和VSTO自动化Powerpoint场景。在我的powerpoint演示文稿中,我在一个形状上创建了一个“Action”设置来启动记事本。在幻灯片放映期间,我需要通过单击“文本/形状”来调用此操作,以便它将打开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];
发布于 2012-01-11 02:55:28
这可能是一个比您期望的更复杂的解决方案,但是如果您能够以某种方式确定屏幕上“文本/形状”对象的X和Y坐标(也许使用编码的UI和VSTO Libraries?),您就可以使用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();
下面是我用来模拟Windows操作的InputEmulator类的精简版本:
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;
}
}
https://stackoverflow.com/questions/8712311
复制相似问题