C# 如何使用 WM_GETTEXT / GetWindowText API / 窗口标题

发布于 2024-12-09 07:57:01 字数 919 浏览 1 评论 0原文

我想获取应用程序的控件/句柄的内容..

这是实验代码..

 Process[] processes = Process.GetProcessesByName("Notepad");
        foreach (Process p in processes)
        {
            StringBuilder sb = new StringBuilder();
            IntPtr pFoundWindow = p.MainWindowHandle;
             List <IntPtr> s =    GetChildWindows(pFoundWindow); 
            // function that returns a 
            //list of handle from child component on a given application.

             foreach (IntPtr test in s)
             {
              // Now I want something here that will return/show 
               the text on the notepad..


             }


            GetWindowText(pFoundWindow, sb,256);
            MessageBox.Show(sb.ToString()); // this shows the title.. no problem with that

        } 

有什么想法吗? 我读过一些 API 方法,如 GetWindowText 或 WM_GETTEXT,但我不知道如何使用它或将其应用到我的代码中。 我需要教程或示例代码...

提前致谢:)

I want to get the content of the control / handle of an application..

Here's the experimental code..

 Process[] processes = Process.GetProcessesByName("Notepad");
        foreach (Process p in processes)
        {
            StringBuilder sb = new StringBuilder();
            IntPtr pFoundWindow = p.MainWindowHandle;
             List <IntPtr> s =    GetChildWindows(pFoundWindow); 
            // function that returns a 
            //list of handle from child component on a given application.

             foreach (IntPtr test in s)
             {
              // Now I want something here that will return/show 
               the text on the notepad..


             }


            GetWindowText(pFoundWindow, sb,256);
            MessageBox.Show(sb.ToString()); // this shows the title.. no problem with that

        } 

any idea?
I've read some API method like GetWindowText or WM_GETTEXT but I dont know how to use it or apply it on my code..
I need a tutorial or sample code...

Thanks in advance : )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

违心° 2024-12-16 07:57:01
public class GetTextTestClass{

    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr SendMessage(int hWnd, int Msg, int wparam, int lparam);

    const int WM_GETTEXT       = 0x000D;
    const int WM_GETTEXTLENGTH = 0x000E;

    public string GetControlText(IntPtr hWnd){

        // Get the size of the string required to hold the window title (including trailing null.) 
        Int32 titleSize = SendMessage((int)hWnd, WM_GETTEXTLENGTH, 0, 0).ToInt32();

        // If titleSize is 0, there is no title so return an empty string (or null)
        if (titleSize == 0)
            return String.Empty;

        StringBuilder title = new StringBuilder(titleSize + 1);

        SendMessage(hWnd, (int)WM_GETTEXT, title.Capacity, title);

        return title.ToString();
    }
}
public class GetTextTestClass{

    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr SendMessage(int hWnd, int Msg, int wparam, int lparam);

    const int WM_GETTEXT       = 0x000D;
    const int WM_GETTEXTLENGTH = 0x000E;

    public string GetControlText(IntPtr hWnd){

        // Get the size of the string required to hold the window title (including trailing null.) 
        Int32 titleSize = SendMessage((int)hWnd, WM_GETTEXTLENGTH, 0, 0).ToInt32();

        // If titleSize is 0, there is no title so return an empty string (or null)
        if (titleSize == 0)
            return String.Empty;

        StringBuilder title = new StringBuilder(titleSize + 1);

        SendMessage(hWnd, (int)WM_GETTEXT, title.Capacity, title);

        return title.ToString();
    }
}
迷途知返 2024-12-16 07:57:01

GetWindowText 不会为您提供其他应用程序的编辑窗口内容 -

您需要使用 SendMessage 的 StringBuilder 版本:

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

const int WM_GETTEXT = 0xD;
StringBuilder sb = new StringBuilder(65535);
// needs to be big enough for the whole text
SendMessage(hWnd_of_Notepad_Editor, WM_GETTEXT, sb.Length, sb);

GetWindowText won't give you the content of edit windows from other applications - it only supports default-managed text [like the captions of labels] across processes to prevent hangs... you'll have to send WM_GETTEXT.

You'll need to use a StringBuilder version of SendMessage:

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

const int WM_GETTEXT = 0xD;
StringBuilder sb = new StringBuilder(65535);
// needs to be big enough for the whole text
SendMessage(hWnd_of_Notepad_Editor, WM_GETTEXT, sb.Length, sb);
卸妝后依然美 2024-12-16 07:57:01

看看 http://pinvoke.net/default.aspx/user32/GetWindowText.html 以及 MSDN 上的文档。下面是如何使用 GetWindowText 方法的简短代码示例。

Have a look at http://pinvoke.net/default.aspx/user32/GetWindowText.html and also the documentation on MSDN. Below you find a short code example how to use the GetWindowText method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文