使用C#获取在其他应用程序的文本框中输入的文本

巴拉斯

我想获取其他应用程序文本框中的文本。它可能是gtalk客户端的文本框或soap UI屏幕。

根据我的研究,大多数论坛建议winapi是实现此目标所必须使用的概念。我没有任何很好的例子来实现这一目标。

弗朗西斯·麦克唐纳

这是一个如何通过窗口标题获取窗口中所有文本的示例。

请参阅评论以获取有关其工作原理的解释。

public class GetWindowTextExample
{
    // Example usage.
    public static void Main()
    {
        var allText = GetAllTextFromWindowByTitle("Untitled - Notepad");
        Console.WriteLine(allText);
        Console.ReadLine();
    }

    // Delegate we use to call methods when enumerating child windows.
    private delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

    [DllImport("user32")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);

    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    private static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, [Out] StringBuilder lParam);

    // Callback method used to collect a list of child windows we need to capture text from.
    private static bool EnumChildWindowsCallback(IntPtr handle, IntPtr pointer)
    {
        // Creates a managed GCHandle object from the pointer representing a handle to the list created in GetChildWindows.
        var gcHandle = GCHandle.FromIntPtr(pointer);

        // Casts the handle back back to a List<IntPtr>
        var list = gcHandle.Target as List<IntPtr>;

        if (list == null)
        {
            throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
        }

        // Adds the handle to the list.
        list.Add(handle);

        return true;
    }

    // Returns an IEnumerable<IntPtr> containing the handles of all child windows of the parent window.
    private static IEnumerable<IntPtr> GetChildWindows(IntPtr parent)
    {
        // Create list to store child window handles.
        var result = new List<IntPtr>();

        // Allocate list handle to pass to EnumChildWindows.
        var listHandle = GCHandle.Alloc(result);

        try
        {
            // Enumerates though all the child windows of the parent represented by IntPtr parent, executing EnumChildWindowsCallback for each. 
            EnumChildWindows(parent, EnumChildWindowsCallback, GCHandle.ToIntPtr(listHandle));
        }
        finally
        {
            // Free the list handle.
            if (listHandle.IsAllocated)
                listHandle.Free();
        }

        // Return the list of child window handles.
        return result;
    }

    // Gets text text from a control by it's handle.
    private static string GetText(IntPtr handle)
    {
        const uint WM_GETTEXTLENGTH = 0x000E;
        const uint WM_GETTEXT = 0x000D;

        // Gets the text length.
        var length = (int)SendMessage(handle, WM_GETTEXTLENGTH, IntPtr.Zero, null);

        // Init the string builder to hold the text.
        var sb = new StringBuilder(length + 1);

        // Writes the text from the handle into the StringBuilder
        SendMessage(handle, WM_GETTEXT, (IntPtr)sb.Capacity, sb);

        // Return the text as a string.
        return sb.ToString();
    }

    // Wraps everything together. Will accept a window title and return all text in the window that matches that window title.
    private static string GetAllTextFromWindowByTitle(string windowTitle)
    {
        var sb = new StringBuilder();

        try
        {
            // Find the main window's handle by the title.
            var windowHWnd = FindWindowByCaption(IntPtr.Zero, windowTitle);

            // Loop though the child windows, and execute the EnumChildWindowsCallback method
            var childWindows = GetChildWindows(windowHWnd);

            // For each child handle, run GetText
            foreach (var childWindowText in childWindows.Select(GetText))
            {
                // Append the text to the string builder.
                sb.Append(childWindowText);
            }

            // Return the windows full text.
            return sb.ToString();
        }
        catch (Exception e)
        {
            Console.Write(e.Message);
        }

        return string.Empty;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用单个文本框在其他框中输入值

来自分类Dev

Vb.net从在其他文本框中逐行插入的文本框中获取文本

来自分类Dev

如何从C#中的文本框获取输入?

来自分类Dev

如何使用Windows窗体应用程序获取文本框中的文件路径?

来自分类Dev

如何使用Python Tkinter在其他框架的文本框中显示过程输出

来自分类Dev

如何避免在Windows应用程序的文本框中输入特殊字符

来自分类Dev

当 WPF 应用程序中的文本框输入更改时,如何更改主窗口颜色?

来自分类Dev

编写一个循环,该循环打印用户在文本框中输入的行数,这是使用C#Windows应用程序

来自分类Dev

如何使用 C# Web 应用程序将选定路径中的文件夹名称读取到文本框中

来自分类Dev

如何限制文本框C#表单应用程序的特定字符数

来自分类Dev

C# 文本框短日期格式异常错误,Windows 窗体应用程序

来自分类Dev

如何使用for循环在c#windows窗体应用程序中检查文本框是否为空,使用数组

来自分类Dev

无法从其他应用程序获取文本(窗口)

来自分类Dev

每次单击C#时,请使用其他文本框或替代方法

来自分类Dev

将文字同时放在其他位置的2个文本框中

来自分类Dev

为应用程序中的所有文本框选择文本框中的所有文本

来自分类Dev

如何从文本文件中读取数据并在其他文本框中拆分?

来自分类Dev

C#在文本框中获取焦点位置/索引

来自分类Dev

从另一个应用程序中的文本框中读取文本

来自分类Dev

当我尝试解析的文本框中未输入任何内容时,应用程序崩溃

来自分类Dev

当我尝试解析的文本框中未输入任何内容时,应用程序崩溃

来自分类Dev

文本框的输入范围在Windows 10通用应用程序中不起作用

来自分类Dev

文本框填充时,jQuery SHOW其他输入

来自分类Dev

如何从其他应用程序中获取所选文本并同时保留剪贴板文本

来自分类Dev

如何从另一个应用程序的文本框中获取插入符的位置?(不是坐标,而是文本框内的实际索引)

来自分类Dev

C# 是否可以在不覆盖文本框的第一个输入值的情况下在一个文本框中获取两个输入文本框值?

来自分类Dev

将同一文本框中的两个值相乘,并用符号分隔,并在其他文本框中显示结果

来自分类Dev

是否可以使用 C# 在桌面窗口表单应用程序中放置 Bing/Google 地图自动完成邮政地址文本框?

来自分类Dev

如何在文本框值中输入当前日期并显示我的其他数据

Related 相关文章

  1. 1

    使用单个文本框在其他框中输入值

  2. 2

    Vb.net从在其他文本框中逐行插入的文本框中获取文本

  3. 3

    如何从C#中的文本框获取输入?

  4. 4

    如何使用Windows窗体应用程序获取文本框中的文件路径?

  5. 5

    如何使用Python Tkinter在其他框架的文本框中显示过程输出

  6. 6

    如何避免在Windows应用程序的文本框中输入特殊字符

  7. 7

    当 WPF 应用程序中的文本框输入更改时,如何更改主窗口颜色?

  8. 8

    编写一个循环,该循环打印用户在文本框中输入的行数,这是使用C#Windows应用程序

  9. 9

    如何使用 C# Web 应用程序将选定路径中的文件夹名称读取到文本框中

  10. 10

    如何限制文本框C#表单应用程序的特定字符数

  11. 11

    C# 文本框短日期格式异常错误,Windows 窗体应用程序

  12. 12

    如何使用for循环在c#windows窗体应用程序中检查文本框是否为空,使用数组

  13. 13

    无法从其他应用程序获取文本(窗口)

  14. 14

    每次单击C#时,请使用其他文本框或替代方法

  15. 15

    将文字同时放在其他位置的2个文本框中

  16. 16

    为应用程序中的所有文本框选择文本框中的所有文本

  17. 17

    如何从文本文件中读取数据并在其他文本框中拆分?

  18. 18

    C#在文本框中获取焦点位置/索引

  19. 19

    从另一个应用程序中的文本框中读取文本

  20. 20

    当我尝试解析的文本框中未输入任何内容时,应用程序崩溃

  21. 21

    当我尝试解析的文本框中未输入任何内容时,应用程序崩溃

  22. 22

    文本框的输入范围在Windows 10通用应用程序中不起作用

  23. 23

    文本框填充时,jQuery SHOW其他输入

  24. 24

    如何从其他应用程序中获取所选文本并同时保留剪贴板文本

  25. 25

    如何从另一个应用程序的文本框中获取插入符的位置?(不是坐标,而是文本框内的实际索引)

  26. 26

    C# 是否可以在不覆盖文本框的第一个输入值的情况下在一个文本框中获取两个输入文本框值?

  27. 27

    将同一文本框中的两个值相乘,并用符号分隔,并在其他文本框中显示结果

  28. 28

    是否可以使用 C# 在桌面窗口表单应用程序中放置 Bing/Google 地图自动完成邮政地址文本框?

  29. 29

    如何在文本框值中输入当前日期并显示我的其他数据

热门标签

归档