首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在C#中从外部应用程序获取UI文本

在C#中,从外部应用程序获取UI文本的方法是通过使用Windows API函数。以下是一个简单的示例,展示了如何获取当前活动窗口的标题栏文本:

代码语言:csharp
复制
using System;
using System.Runtime.InteropServices;
using System.Text;

public class Win32API
{
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
}

public class Program
{
    public static void Main()
    {
        IntPtr hWnd = Win32API.GetForegroundWindow();
        StringBuilder title = new StringBuilder(256);
        Win32API.GetWindowText(hWnd, title, 256);
        Console.WriteLine("Active window title: " + title.ToString());
    }
}

这个示例中,我们使用了GetForegroundWindow函数来获取当前活动窗口的句柄,然后使用GetWindowText函数来获取窗口标题栏的文本。

需要注意的是,这种方法只能获取到当前活动窗口的标题栏文本,而不能获取到其他窗口的文本。此外,如果目标窗口使用了自定义控件来显示文本,那么这种方法可能无法获取到这些文本。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

1分51秒

Ranorex Studio简介

8分7秒

06多维度架构之分库分表

22.2K
2分43秒

ELSER 与 Q&A 模型配合使用的快速演示

16分8秒

人工智能新途-用路由器集群模仿神经元集群

领券