2012-01-03 22 views
16

Tôi đang tự động hóa một kịch bản Powerpoint sử dụng Giao diện người dùng được mã hóa & VSTO. Trong bài thuyết trình powerpoint của tôi, tôi đã tạo một thiết lập 'Hành động' trên một hình dạng để khởi động notepad. Trong khi trình chiếu, tôi cần gọi hành động này bằng cách nhấp vào 'văn bản/hình dạng' để nó sẽ mở notepad.exe. Bất cứ ai có thể giúp tôi làm thế nào để đạt được điều này. Tôi đã viết mã sau đây.Làm thế nào để gọi một hành động trong khi trình chiếu powerpoint theo trình tự?

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

Điều này sẽ khởi chạy trình chiếu cho bản trình bày và trang chiếu thứ nhất 'nơi cài đặt hành động được xác định trên hình dạng' sẽ được hiển thị. Bây giờ làm thế nào tôi có thể khởi động notepad.exe tự động thông qua API? giao diện người dùng được mã hóa không may không thể phát hiện các đối tượng trong một trang trình bày. Vì vậy, một tùy chọn nhấp chuột giao diện người dùng có thể không thực hiện được.

[Chỉnh sửa] Có thể thực hiện một chút tiến bộ. Tôi đã có đối tượng hình dạng trong khi trình chiếu. Đây là phần mở rộng cho mã trên.

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

Tôi không rõ ràng trên những gì bạn đang cố gắng hoàn thành. Nếu bạn muốn tạo một bản trình bày, hãy khởi chạy nó trong dạng xem trình chiếu và sau đó bắt đầu notepad, tại sao nó lại thông qua PowerPoint? Yêu cầu mã của bạn khởi chạy notepad sau khi đã tạo và khởi chạy chương trình PPT. –

+0

Đây là một kịch bản tự động hóa để xác minh hành động đang hoạt động đúng cách. Do đó tôi phải làm theo cách này chỉ – satya

+0

Tôi hiểu. Tôi không biết cách nào để tự động hóa một cú nhấp chuột trên bất kỳ hình dạng hoặc điểm cụ thể nào trên màn hình. –

Trả lời

5

Điều này có thể là một giải pháp phức tạp hơn những gì bạn đã mong đợi, nhưng nếu bạn nào đó có thể xác định X và Y tọa độ "text/hình dạng" đối tượng của bạn trên màn hình (có lẽ với giao diện người dùng Coded và VSTO Libraries?), Bạn có thể sử dụng phương thức User32 "SendInput" để mô phỏng di chuyển chuột đến vị trí của đối tượng và sau đó mô phỏng một cú click chuột.

Đây là đoạn mã để thi đua đầu vào của người dùng:

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

Và đây là phiên bản rút gọn của một lớp InputEmulator tôi sử dụng để mô phỏng Windows UI hành động:

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

Cảm ơn nomizzz. Có vẻ đầy hứa hẹn. Tôi sẽ cố gắng thực hiện điều này khi tìm kiếm giải pháp dựa trên SDK của tôi trở nên vô ích. – satya

7

Đừng hỏi tôi tại sao C# cư xử như thế này nhưng nó!

Bạn cần phải ban hành lệnh hai lần cho nó hoạt động ...

thử và thử nghiệm

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

Xin lỗi Siddhart .. Những gì tôi đang tìm kiếm là làm thế nào để 'gọi' hành động trong khi trình chiếu của bài thuyết trình. Mã của bạn chỉ giúp cách đặt cài đặt hành động trên hình dạng mà tôi đã biết. Hope u có sự khác biệt. Tôi vẫn đang tìm câu trả lời. – satya