需要做pc的自动化操作。
一个winform的软件,鼠标悬停到某个表格的单元格上,有个tooltip,我需要捕获那个tootip的文字信息。
如图“被引用文件中的问题”这样的字样。
https://learn.microsoft.com/zh-cn/dotnet/framework/ui-automation/ui-automation-support-for-the-tooltip-control-type
网上的资料很少,只有这一篇,也没好到解决方案。
谢谢!
需要做pc的自动化操作。
一个winform的软件,鼠标悬停到某个表格的单元格上,有个tooltip,我需要捕获那个tootip的文字信息。
如图“被引用文件中的问题”这样的字样。
https://learn.microsoft.com/zh-cn/dotnet/framework/ui-automation/ui-automation-support-for-the-tooltip-control-type
网上的资料很少,只有这一篇,也没好到解决方案。
谢谢!
引用new bing部分回答作答:
在 C# 的 UIAutomation 中,要获取 Tooltip 的文字信息,可以通过以下步骤:
1 首先使用 UIAutomation 查找到对应的表格单元格元素。可以使用 FindFirst() 或 FindAll() 方法,通过元素的 Name、AutomationId、ClassName、ControlType 等属性进行查找。例如:
var tableCell = window.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "单元格名称"));
2 一旦找到了表格单元格元素,可以使用 GetUpdatedCacheRequest() 方法来获取当前 UIAutomation 缓存中的元素信息。例如:
var cacheRequest = new CacheRequest();
cacheRequest.Add(AutomationElement.NameProperty);
cacheRequest.Add(ValuePattern.ValueProperty);
cacheRequest.Add(ValuePattern.IsReadOnlyProperty);
cacheRequest.Add(ValuePattern.IsPasswordProperty);
cacheRequest.TreeScope = TreeScope.Element | TreeScope.Descendants;
cacheRequest.AutomationElementMode = AutomationElementMode.Full;
var cachedElement = Automation.Cached.GetUpdatedCacheRequest(cacheRequest).FindFirst(
TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "单元格名称"));
3 如果表格单元格上有 Tooltip,可以使用 AutomationElement.ToolTipOpenedEvent 和 AutomationElement.ToolTipClosedEvent 事件来检测 Tooltip 的打开和关闭。例如:
var tooltip = Automation.AddAutomationEventHandler(
AutomationElement.ToolTipOpenedEvent, tableCell, TreeScope.Element, (sender, e) =>
{
// 在此处获取 Tooltip 的信息
});
4 在 tooltip 事件处理程序中,可以使用 e.SourceElement 属性来获取打开 Tooltip 的元素。可以使用 GetUpdatedCacheRequest() 方法来获取 Tooltip 元素的缓存信息。例如:
var tooltipElement = e.SourceElement.GetUpdatedCacheRequest(cacheRequest);
var tooltipText = tooltipElement.GetCurrentPropertyValue(ValuePattern.ValueProperty) as string;
为了确保能够正确捕获 Tooltip 的信息,需要在代码中加入适当的延时或等待操作,以确保 Tooltip 已经完全打开并加载了其内容。