简体   繁体   English

如何从Windows控件的文本框中获取文本? 我怎么知道这是一个文本框?

[英]How to get text from a windows control's textbox? and how do I know it's a textbox?

I've used spy++ to find the right handle of the wanted windows control, which belongs to a standalone application which isn't managed. 我使用spy ++来查找所需Windows控件的正确句柄,该控件属于不受管理的独立应用程序。 Note that the spy++ "property inspector" mentions that this window doesn't have any child (or parent) windows. 请注意,spy ++“属性检查器”提到此窗口没有任何子(或父)窗口。

I've also managed to get back the name of the window with the following code: 我还设法通过以下代码获取了窗口的名称:

   //the invokes are included aswell
    const int WM_GETTEXT = 0x000D;
    static void Main(string[] args)
    {
        IntPtr handle = new IntPtr(Convert.ToInt32("00070818", 16)); 
        int nChars = GetWindowTextLength(handle); //win32 function
        int length = 200;
        StringBuilder sb = new StringBuilder(length);
        SendMessage(handle, WM_GETTEXT, length, sb);
        Console.WriteLine(sb.ToString());           

    }

This window has a lot more information than it's title, and that's all I seem to get back with WM_GETTEXT (changing the value of length to 200 didnt help, was a long shot anyway). 这个窗口除了标题外还有很多信息,这似乎是我回想起的WM_GETTEXT(将length的值更改为200并没有帮助,反而是一个漫长的尝试)。

Next, I've tried a different approach using UI Automation: 接下来,我尝试了使用UI自动化的另一种方法:

static void Main(string[] args)
{
    AutomationElement target = AutomationElement.FromHandle(handle);
    TextPattern textPattern = target.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
}

but I got this error back: 但是我得到了这个错误:

An unhandled exception of type 'System.InvalidOperationException' occurred in UIAutomationClient.dll UIAutomationClient.dll中发生了类型为'System.InvalidOperationException'的未处理异常

Additional information: Unsupported Pattern. 附加信息:不支持的模式。

To top it off, I've also tried using Microsoft Inspect.exe, but it failed to focus on the text in the window, both in UI automation and MSAA mode. 最重要的是,我还尝试使用Microsoft Inspect.exe,但是在UI自动化和MSAA模式下,它都无法专注于窗口中的文本。


Does it mean that the data cant be achieved with UI Automation?? 这是否意味着使用UI自动化无法实现数据? or should am I just using the wrong methods/types? 还是我应该使用错误的方法/类型?

Is there another way to get the data from this window other than using GetWindowText, WM_GETTEXT, or UI automation?? 除了使用GetWindowText,WM_GETTEXT或UI自动化之外,还有其他方法可以从此窗口获取数据吗?

I'm fairly new to this stuff, but I'm trying my best to learn. 我对这些东西还很陌生,但是我正在努力学习。 In addition I've have no current leads so any helpful comment/answer would be much appreciated!! 另外,我目前没有潜在客户,因此任何有用的评论/答案将不胜感激! if you do answer please be sure to include helpful keywords so i'll be able to learn more about your solutions 如果您确实要回答, 请务必添加有用的关键字,这样我就可以了解有关您的解决方案的更多信息

Labels (ie static controls) and text boxes are child windows with their own handles, hence they are visible to Spy++. 标签(即静态控件)和文本框是带有它们自己的句柄的子窗口,因此它们对于Spy ++是可见的。 If your target Window has no children then it's not using a label or text box, it's painting the text itself, and you won't be able to retrieve it using GetWindowText or WM_GETTEXT . 如果目标窗口没有子窗口,则它不使用标签或文本框,而是在绘制文本本身,并且您将无法使用GetWindowTextWM_GETTEXT检索它。

The text might be exposed through UI Automation , the API used by screen readers. 文本可以通过UI Automation (屏幕阅读器使用的API)公开。 Use UISpy.exe or Inspect.exe to see if the text is accessible. 使用UISpy.exe或Inspect.exe查看文本是否可访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM