在使用Windows API方法EnumChildWindows时,我会遇到奇怪的行为.似乎没有拿起一段儿童窗户.当我使用Spy ++向下钻取时,我可以看到孩子们,但是当我执行我的代码时,它不会返回我在Spy ++中看到的代码.
我在Spy ++中 看到的内容我在Spy ++中看到的内容http://img24.imageshack.us/img24/9264/spyhandles.png
这是我的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
有没有理由为什么List<IntPtr>在调用EnumChildWindows时,上面屏幕截图中突出显示的红色部分不会填充到我的collection()中?
| 归档时间: |
|
| 查看次数: |
8540 次 |
| 最近记录: |