tof*_*tim 7 c# powerpoint automation ui-automation
该代码适用于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;
}
}
Run Code Online (Sandbox Code Playgroud)
您无法从屏幕截图中看到它,但鼠标处于"先"而不是"卡住",但无论鼠标放在何处,它总是卡住.也许这是在PowerPoint 2016中修复的?
当我查看范围的边界框时,它始终是整个元素,而不是选定的单词.这可能是RangeToPoint无法正常工作的问题的一部分.
原帖在MSDN上发布但没有回复......
更新.如果我使用
text = printRange(range, text);
while (range.Move(TextUnit.Word, 1) > 0)
{
text += Environment.NewLine;
text = printRange(range, text);
}
Run Code Online (Sandbox Code Playgroud)
我明白了
小智 3
此行为可能是由于 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 中是否已修复此问题,我会尝试调查并通知您。
谢谢,
盖伊
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) + "\"";
}
}
}
Run Code Online (Sandbox Code Playgroud)