用监听器添加形状

本文关键字:添加 监听器 | 更新日期: 2023-09-27 18:15:00

我正试图在c#中为powerpoint编写一个插件,该插件创建一个新的幻灯片并添加文本框,当单击执行特定操作时。目前,我已经得到了一个操作工作,它打开记事本,但我希望它调用另一个函数代替。

当前代码如下所示:

PowerPoint.Shape sendQuestion = slide.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, slideWidth - 200, slideHeight - 50, 200, 50);
sendQuestion.Name = "sendQuestion";
sendQuestion.TextFrame.TextRange.InsertAfter("Send Question");
sendQuestion.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionRunProgram;
sendQuestion.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = @"C:'WINDOWS'system32'notepad.exe";

用监听器添加形状

在ThisAddIn_Startup方法中,初始化Application_WindowSelectionChange事件处理程序。

public void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.WindowSelectionChange += Application_WindowSelectionChange;
}
对于事件处理程序,您可以使用以下代码-
public void Application_WindowSelectionChange(PowerPoint.Selection sel)
{
    if (sel.Type == PowerPoint.PpSelectionType.ppSelectionShapes)
    {
        if (sel.ShapeRange.Name == "MyTextBox")
        {
         //Perform certain action.
        }
    }
}

同样在ThisAddIn_Shutdown方法中,您需要这样做-

public void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.WindowSelectionChange -= Application_WindowSelectionChange;
}