是否可以从 C# 中的外部应用程序获取 UI 文本。
特别是,有没有办法从第三方编写的外部 Win32 应用程序的标签(我假设它是普通的 Windows 标签控件)读取 Unicode 文本?文本可见,但无法在 UI 中通过鼠标选择。
我假设有一些可访问性 API(例如,用于屏幕阅读器)允许这样做。
编辑:目前正在考虑使用类似的东西托管间谍应用程序但仍然会感谢任何其他线索。
如果您只关心标准 Win32 标签,那么WM_GETTEXT会很好地工作,如其他答案中所述。
--
有一个辅助功能 API -UI自动化- 对于标准标签,它也在幕后使用 WM_GETTEXT。然而,它的一个优点是它可以从几种其他类型的控件获取文本,包括大多数系统控件,以及通常使用非系统控件的 UI - 包括 WPF、IE 和 Firefox 中的文本等。
// compile as:
// csc file.cs /r:UIAutomationClient.dll /r:UIAutomationTypes.dll /r:WindowsBase.dll
using System.Windows.Automation;
using System.Windows.Forms;
using System;
class Test
{
public static void Main()
{
// Get element under pointer. You can also get an AutomationElement from a
// HWND handle, or by navigating the UI tree.
System.Drawing.Point pt = Cursor.Position;
AutomationElement el = AutomationElement.FromPoint(new System.Windows.Point(pt.X, pt.Y));
// Prints its name - often the context, but would be corresponding label text for editable controls. Can also get the type of control, location, and other properties.
Console.WriteLine( el.Current.Name );
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)