在C#中从外部应用程序获取UI文本 [英] Getting UI text from external app in C#

查看:288
本文介绍了在C#中从外部应用程序获取UI文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能从C#中的外部应用程序获取UI文本.

Is it possible to get UI text from an external application in C#.

特别是,是否有一种方法可以从由第三方编写的外部Win32应用程序从标签(我认为它是普通的Windows标签控件)中读取Unicode文本?文本是可见的,但是在UI中无法通过鼠标选择.

In particular, is there a way to read Unicode text from a label (I assume it's a normal Windows label control) from an external Win32 app that was written by a 3rd party? The text is visible, but not selectable by mouse in the UI.

我假设有一些可访问性API(例如,供屏幕阅读器使用)允许该操作.

I assume there is some accessibility API (e.g. meant for screen readers) that allows this.

目前正在研究使用托管间谍应用,但仍然会感激其他线索.

Currently looking into using something like the Managed Spy App but would still appreciate any other leads.

推荐答案

如果您只关心标准Win32标签,则

If you just care about the standard Win32 label, then WM_GETTEXT will work fine, as outlined in the other answers.

-

有一个可访问性API- UIAutomation -用于标准标签,它也在后台使用WM_GETTEXT.但是,它的一个优点是,它可以从其他几种类型的控件(包括大多数系统控件)以及通常使用非系统控件的UI中获取文本-包括WPF,IE和Firefox中的文本,以及其他.

There is an accessibility API - UIAutomation - for standard labels, it too uses WM_GETTEXT behind the scenes. One advantage to it, however, is that it can get text from several other types of controls, including most system controls, and often UI using non-system controls - including WPF, text in IE and Firefox, and others.

// 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 );
    }
}

这篇关于在C#中从外部应用程序获取UI文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆