为什么EnumChildWindows逃课的孩子吗? [英] Why is EnumChildWindows skipping children?

查看:157
本文介绍了为什么EnumChildWindows逃课的孩子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到奇怪的现象,当涉及到使用Windows API方法EnumChildWindows。它似乎不能拾取孩子窗口的部分。当我深入使用间谍+ +我可以看到孩子们,但是当我执行我的code,它不会返回那些我在间谍++看到的。

I'm getting strange behavior when it comes to using the Windows API method EnumChildWindows. It seems to not be picking up a section of children windows. When I drill down using Spy++ I can see the children, but when I execute my code, it doesn't return the ones I see in Spy++.

我看到间谍++什么

What I see in Spy++

下面是我的code:

public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

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

    public static List<IntPtr> GetChildWindows(IntPtr parent)
    {
        List<IntPtr> result = new List<IntPtr>();
        GCHandle listHandle = GCHandle.Alloc(result);
        try
        {
            EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
            EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
        }
        finally
        {
            if (listHandle.IsAllocated)
                listHandle.Free();
        }
        return result;
    }

    private static bool EnumWindow(IntPtr handle, IntPtr pointer)
    {
        GCHandle gch = GCHandle.FromIntPtr(pointer);
        List<IntPtr> list = gch.Target as List<IntPtr>;
        if (list == null)
            throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");

        list.Add(handle);            
        return true;
    }

有一个原因,为什么在上面的截图中突出显示红色的部分将不会被填充到我的收藏(列表&LT; IntPtr的&GT; )?调用EnumChildWindows时

Is there a reason as to why the highlighted red section in the screenshot above would not get populated into my collection (List<IntPtr>) when calling EnumChildWindows?

推荐答案

卫生署!我发现我的方式错误。我之所以只得到一半的孩子是由于我没有等待足够长的时间窗口开始加载,并创造一切在它的孩子的事实,所以我只是得到上半年,它在创建当时我打电话给我的函数来获取所有的子窗口。所以,我添加了一行code的调用EnumChildWindows()之前入睡。

Doh! I discovered the errors of my ways. The reason why I was only getting half of the children was due to the fact I was not waiting long enough for the window to initially load and create ALL of the children within it, therefore I was only getting the first half that it created at the time I was calling my function to obtain all child windows. So I added a line of code to sleep before calling EnumChildWindows().

Windows不调用回调函数EnumChildWindows被调用后创建的子窗口,但在返回之前。 - <一个href=\"http://www.c-sharpcorner.com/UploadFile/shrijeetnair/win32api12062005005528AM/win32api.aspx\">Source

"Windows does not call the callback function for any child windows created after EnumChildWindows is called but before it returns." - Source

信息的上面引述的部分是什么结果光球在我的头上。

The above quoted piece of information is what turned the light-bulb on in my head.

这篇关于为什么EnumChildWindows逃课的孩子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆