相关疑难解决方法(0)

当我尝试使用UI Automation的UI自动化时,我只能在使用RangeFromPoint时获得第一个字符/单词

该代码适用于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 …
Run Code Online (Sandbox Code Playgroud)

c# powerpoint automation ui-automation

7
推荐指数
1
解决办法
646
查看次数

标签 统计

automation ×1

c# ×1

powerpoint ×1

ui-automation ×1