C#PInvoke从已知窗口中查找子窗口的窗口句柄

时间:2011-06-16 15:55:04

标签: c# pinvoke

我目前正试图通过C#pinvoke使用SendMessage从子窗口获取一些文本。但是,我之前尝试硬化窗口句柄失败,因为值在应用程序启动时发生了变化。有没有办法可靠地获取此子窗口的窗口句柄? Winspector间谍显示该窗口的类名是RichEdit20W。我目前的代码如下:

IntPtr hWnd= (IntPtr) 0xA0E88; // Hardcode window handle


        int txtlen = SendMessage(hWnd, WM_GETTEXTLENGTH, 20, null);
        StringBuilder text = new StringBuilder(txtlen);
        int RetVal = SendMessage(hWnd, WM_GETTEXT, text.Capacity, text);

3 个答案:

答案 0 :(得分:2)

我最终使用Managed Windows API来枚举窗口的所有后代窗口。

            var descendantwindows = childWindows[0].AllDescendantWindows; // Get all descendant windows of CMainWindow

        for (int i = 0; i<descendantwindows.Length; i++)
        {
            if (descendantwindows[i].ClassName == "RichEdit20W")
                childHandle = descendantwindows[i].HWnd;
        }

答案 1 :(得分:1)

答案 2 :(得分:0)