Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未从PowerPoint 2007捕获UI自动化事件_C#_.net_Powerpoint_Microsoft Ui Automation - Fatal编程技术网

C# 未从PowerPoint 2007捕获UI自动化事件

C# 未从PowerPoint 2007捕获UI自动化事件,c#,.net,powerpoint,microsoft-ui-automation,C#,.net,Powerpoint,Microsoft Ui Automation,当用户选择一个新的功能区选项卡时,我试图从PowerPoint 2007(或理想的任何版本)中捕获UI自动化事件。使用SDK工具Inspect和AccEvent,我确定捕获这些事件的合理“父”元素是“Ribbon Tabs”元素 当我将AccEvent作用域设置为该元素,并在自动化事件中注册SelectionItem\u ElementSelected时,我会像预期的那样获得事件—单击选项卡时,AccEvent会捕获并记录它 我只允许发布两个链接,还不能内联图片,因此我制作了一些马赛克,试图将尽

当用户选择一个新的功能区选项卡时,我试图从PowerPoint 2007(或理想的任何版本)中捕获UI自动化事件。使用SDK工具Inspect和AccEvent,我确定捕获这些事件的合理“父”元素是“Ribbon Tabs”元素

当我将AccEvent作用域设置为该元素,并在自动化事件中注册SelectionItem\u ElementSelected时,我会像预期的那样获得事件—单击选项卡时,AccEvent会捕获并记录它

我只允许发布两个链接,还不能内联图片,因此我制作了一些马赛克,试图将尽可能多的相关信息压缩到每个链接中,以下是与上述行为相关的链接:

基于此,我想出了以下代码来从我的程序中捕获这些事件:

// Prior code gets foreground window, determines if it's PPT, and gets a handle to it
currentApp = AutomationElement.FromHandle(foregroundWindow);

// Create condition to find the "Ribbon Tabs" element
Condition propCondition = new PropertyCondition(
  AutomationElement.NameProperty, "Ribbon Tabs",
  PropertyConditionFlags.IgnoreCase);

// Subscribe to events on the "Ribbon Tabs" Element
SubscribeToEvents(currentApp.FindFirst(TreeScope.Descendants, propCondition));

public void SubscribeToEvents(AutomationElement element)
{
  if (element != null)
  {
    Console.WriteLine("Subscribing to PowerPoint UIA Events on object {0} ({1})",
      elementItem.GetCurrentPropertyValue(AutomationElement.NameProperty),
      elementItem.GetCurrentPropertyValue(AutomationElement.AutomationIdProperty));
    UIAeventHandler = new AutomationEventHandler(OnUIAutomationEvent);

    // Subscribe to SelectionItemPattern.ElementSelectedEvent based off AccEvent
    Automation.AddAutomationEventHandler(
      SelectionItemPattern.ElementSelectedEvent,
      element,
      TreeScope.Descendants,
      UIAeventHandler);

    Console.WriteLine("Subscribed to PowerPoint UIA Events");
  }
}

private void OnUIAutomationEvent(object src, AutomationEventArgs e)
{
  // Make sure the element still exists
  AutomationElement sourceElement;
  try
  {
    sourceElement = src as AutomationElement;
  }
  catch (ElementNotAvailableException)
  {
    return;
  }
  Console.WriteLine("UIA Event ( {0} ) for item: {1}",
    e.EventId.ProgrammaticName,
    sourceElement.GetCurrentPropertyValue(AutomationElement.NameProperty));
}
这段代码不会产生任何结果

如果我订阅了顶层的“窗口”,仍然什么都没有

如果我只是订阅顶级自动化元素,我确实会得到预期的事件——但有一个陷阱。在AccEvent中,事件仅在单击选项卡时显示,真正的“选中”。当我绑定到根AutomationElement时,我在mouseover/hover上获得事件,而在click上什么都没有。我需要事件只在实际选中选项卡时出现(这正是AccEvent在作用域为“Ribbon Tabs”元素时显示的行为)

结果链接:


我需要一种方法来通知我的.NET应用程序,当用户在功能区上选择一个新选项卡时,我是否遗漏了一些明显的内容?

Hi@rex remus找到答案了吗???我有一个类似的要求:我没有。我想得到你们正在寻找的形状上的类似事件。我不确定外部操作的C#是否可以通过MSUIA获得这些信息——至少在不调用大量非托管代码的情况下,可能会截获更低级别的窗口交互(无论如何,这不是MSUIA)。如果你成功了,我很感兴趣。我现在也在研究同样的事情。您是否介意发布实际有效的代码,以便我们进行比较?此外,任何选项卡上都没有AutomationId-你为什么要得到它?上面的代码基本上就是它。可悲的是,这是以前一家公司的老项目,我再也无法访问代码了。这个项目最终由于各种其他原因而停止了,而在这之前,我一直无法获得我想要的自动化事件。我祝你好运,如果你能用你可能遇到的任何可能的解决方案来更新这篇文章,对社区来说将是一件好事。