首页 > 解决方案 > 使用 TestStack.White 获取插入符号位置

问题描述

我正在使用White这样的文本框来获取 Microsoft Word 窗口和文档:

Application a = Application.Attach(0x0000AC34);
Window w = a.GetWindow("Document1 - Word", InitializeOption.WithCache);
TextBox doc = w.Get<TextBox>(SearchCriteria.ByControlType(ControlType.Document));

有没有办法获得插入符号的位置?我对屏幕坐标和文本中的位置都感兴趣。

理想情况下,我正在寻找White.

如果没有,我知道我可以依靠AutomationElementTextPattern即。

TextPattern tp = doc.AutomationElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
TextPatternRange[] selectionRanges = tp.GetSelection();
// etc...

这样做有什么缺点吗?我应该考虑更好的选择吗?

有没有办法可以访问IUIAutomationTextPattern2界面?

标签: c#ui-automationmicrosoft-ui-automationwhite-framework

解决方案


您将无法在 TestStack.White 中执行此操作,因为 TestStack.White 仅包装托管 UIAv2 API。使用FlaUI,您将能够做到这一点,因为他们有一个适用于 UIAv2 和 UIAv3 的包。这是他们围绕 UIAv3 COM dll 编写的包装器。


推荐阅读