2012-01-03 8 views
16

Estoy automatizando un escenario de Powerpoint usando la interfaz de usuario codificada & VSTO. En mi presentación de PowerPoint, he creado una configuración de 'Acción' en una forma para iniciar el Bloc de notas. Durante la presentación de diapositivas, debo invocar esta acción haciendo clic en "texto/forma" para que se abra notepad.exe. ¿Alguien podría ayudarme a cómo lograr esto? Escribí el siguiente código.¿Cómo invocar una acción durante la presentación de PowerPoint mediante programación?

//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(); 

Esto iniciará la presentación de diapositivas para la presentación y la primera diapositiva 'donde las configuraciones de acción se definen de la forma' será mostrado. Ahora, ¿cómo puedo iniciar notepad.exe automáticamente a través de las API? desafortunadamente, la IU codificada no puede detectar objetos en una diapositiva. Por lo tanto, una opción de clic de mouse en la interfaz de usuario puede no ser posible.

[Editar] Capaz de progresar un poco. Obtuve objeto de forma durante la presentación de diapositivas. Esta es la extensión del código anterior.

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

estoy claro en lo que estás tratando de lograr Si desea crear una presentación, ejecútela en la vista de diapositivas y luego inicie el bloc de notas. ¿Por qué hacerlo a través de PowerPoint? Haga que su código abra el bloc de notas después de haber creado y lanzado el programa PPT. –

+0

Este es un escenario de automatización para verificar que la acción funciona correctamente. Por lo tanto, tengo que hacerlo de esta manera solo – satya

+0

veo. No sé de ninguna manera para automatizar un clic en cualquier forma o punto en particular en la pantalla. –

Respuesta

5

Esto podría ser una solución más complicada de lo que usted esperaba, pero si de alguna manera podría determinar la coordenadas X e Y de la "/ forma de texto" objeto en la pantalla (tal vez con la Coded UI y ¿Bibliotecas VSTO?), Puede usar el método User32 "SendInput" para emular el movimiento del mouse a la ubicación del objeto y luego emular un clic del mouse.

Este es el código para emular la entrada del usuario:

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(); 

Y aquí es una versión simplificada de una clase InputEmulator utilizo para emular las acciones de Windows de interfaz de usuario:

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; 
    } 
} 
+0

Gracias nomizzz. Se ve prometedor. Intentaré implementar esto una vez que mi búsqueda de una solución basada en SDK se vuelva inútil. – satya

7
no hacer

pregúntame por qué C# se comporta así, pero lo hace!

Usted tiene que emitir el comando dos veces para que funcione ...

de probada eficacia

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(); 

    } 

HTH

Sid

+0

Lo siento Siddhart .. Lo que estoy buscando es cómo 'invocar' la acción durante la presentación de diapositivas de la presentación. Tu código solo ayuda a establecer una configuración de acción en una forma que ya conozco. Espero que tengas la diferencia. Todavía estoy buscando la respuesta. – satya

Cuestiones relacionadas