该代码适用于 Word 和 Outlook,但不适用于 PowerPoint,因为仅选择文本框的第一个字符或第一个单词。这是一个错误吗?有什么解决方法吗?在 PowerPoint 2013 中的简单 PowerPoint 幻灯片上尝试此操作。
private static async Task<string> getText(double x, double y)
{
string result = null;
try
{
var location = new System.Windows.Point(x, y);
AutomationElement element = AutomationElement.FromPoint(location);
object patternObj;
if (element.TryGetCurrentPattern(TextPattern.Pattern, out patternObj))
{
var textPattern = (TextPattern)patternObj;
var range = textPattern.RangeFromPoint(location);
range.ExpandToEnclosingUnit(TextUnit.Word);
range.Select();
var text = range.GetText(-1).TrimEnd('\r');
return text.Trim();
}
else
{
return "no text found";
}
}
catch (Exception ex)
{
return ex.Message;
}
}
从截图中看不出来,但鼠标是在“第一”而不是“卡住”,但无论鼠标放在哪里,它总是卡住。也许这在 PowerPoint 2016 中已修复?
当我查看范围的边界框时,它始终是整个元素,而不是选定的单词。这可能是 RangeToPoint 不起作用的部分原因。
原文发表于MSDN但没有回应...
Update.如果我使用
text = printRange(range, text);
while (range.Move(TextUnit.Word, 1) > 0)
{
text += Environment.NewLine;
text = printRange(range, text);
}
I get
此行为可能是由于 PowerPoint 2013 中的限制造成的,我预计您无法使用 UIA 解决此问题。当您调用 RangeFromPoint() 时,UIA 提供程序点击鼠标下方(即实现 IUIAutomationTextPattern::RangeFromPoint() 的提供程序),旨在返回鼠标光标所在的退化(即空)范围。然后UIA客户端可以扩展返回的范围以获得周围的字符、单词、行或段落。
但是,正如您所指出的,PowerPoint 2013 并没有这样做。我刚刚编写了下面的测试代码(使用 tlbimp.exe 生成的本机 Windows UIA API 的托管包装器),并发现 PowerPoint 显然为光标下方的整个文本框返回了 TextRange。当我运行代码时,我发现在写字板、Word 2013 和 PowerPoint OnLine 中确实在光标下方得到了预期的单词,但在 PowerPoint 2013 中却没有。当我运行作为 Inspect SDK 一部分的文本资源管理器工具时,我得到了相同的结果工具。下图显示了当鼠标悬停在其中一个单词上时,文本资源管理器报告从 PowerPoint 2013 返回的文本是文本框中的整个文本。
(我应该补充一点,为了让下面的测试代码完全正常工作,我认为当前的显示缩放设置需要为 100%。我没有添加代码来说明其他一些缩放处于活动状态。)
我不知道 PowerPoint 2016 中是否已修复此问题,我会尝试调查并通知您。
Thanks,
Guy
private void buttonGetTheText_Click(object sender, EventArgs e)
{
labelText.Text = "No text found.";
IUIAutomation uiAutomation = new CUIAutomation8();
Point ptCursor = Cursor.Position;
tagPOINT pt;
pt.x = ptCursor.X;
pt.y = ptCursor.Y;
// Cache the Text pattern that's available through the element beneath
// the mouse cursor, (if the Text pattern's supported by the element,) in
// order to avoid another cross-process call to get the pattern later.
int patternIdText = 10014; // UIA_TextPatternId
IUIAutomationCacheRequest cacheRequestTextPattern =
uiAutomation.CreateCacheRequest();
cacheRequestTextPattern.AddPattern(patternIdText);
// Now get the element beneath the mouse.
IUIAutomationElement element =
uiAutomation.ElementFromPointBuildCache(pt, cacheRequestTextPattern);
// Does the element support the Text pattern?
IUIAutomationTextPattern textPattern =
element.GetCachedPattern(patternIdText);
if (textPattern != null)
{
// Now get the degenerative TextRange where the mouse is.
IUIAutomationTextRange range = textPattern.RangeFromPoint(pt);
if (range != null)
{
// Expand the range to include the word surrounding
// the point where the mouse is.
range.ExpandToEnclosingUnit(TextUnit.TextUnit_Word);
// Show the word in the test app.
labelText.Text = "Text is: \"" + range.GetText(256) + "\"";
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)