博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EnumChildWindows (user32)
阅读量:7005 次
发布时间:2019-06-27

本文共 3849 字,大约阅读时间需要 12 分钟。

  hot3.png

C# Signature:

[DllImport("user32.dll")]

[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);

public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);

[DllImport("user32.Dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback,IntPtr lParam);

VB.NET Signature:

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _

Private Shared Function EnumChildWindows(ByVal hWndParent As System.IntPtr, ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Integer) As Boolean
End Function

User-Defined Types:

Notes:

The corresponding unmanaged return type is a 4-byte Win32 'BOOL', so mark the method with the MarshalAsAttribute(.Bool).

Tips & Tricks:

has a nice article

Sample Code (C#):

    [DllImport("user32")]

    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
    /// <summary>
    /// Returns a list of child windows
    /// </summary>
    /// <param name="parent">Parent of the windows to return</param>
    /// <returns>List of child windows</returns>
    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;
    }
    /// <summary>
    /// Callback method to be used when enumerating windows.
    /// </summary>
    /// <param name="handle">Handle of the next window</param>
    /// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param>
    /// <returns>True to continue the enumeration, false to bail</returns>
    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);
    //  You can modify this to check to see if you want to cancel the operation, then return a null here
    return true;
    }
    /// <summary>
    /// Delegate for the EnumChildWindows method
    /// </summary>
    /// <param name="hWnd">Window handle</param>
    /// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param>
    /// <returns>True to continue enumerating, false to bail.</returns>
    public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

Sample Code (VB.net):

    Imports System.Runtime.InteropServices

    Public Class NativeMethods
    <DllImport("User32.dll")> _
    Private Shared Function EnumChildWindows _
        (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, _
        ByVal lParam As IntPtr) As Boolean
    End Function
    Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
    Public Shared Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
        Dim ChildrenList As New List(Of IntPtr)
        Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
        Try
        EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
        Finally
        If ListHandle.IsAllocated Then ListHandle.Free()
        End Try
        Return ChildrenList.ToArray
    End Function
    Private Shared Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
        Dim ChildrenList As List(Of IntPtr) = GCHandle.FromIntPtr(Parameter).Target
        If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)")
        ChildrenList.Add(Handle)
        Return True
    End Function
    End Class

Alternative Managed API:

The project () provides a

method ManagedWinapi.SystemWindow.FilterDescendantWindows().

转载于:https://my.oschina.net/tangyu/blog/83041

你可能感兴趣的文章
CentOS7 以下安装Mysql MMM
查看>>
windows系统里Cygwin中如何正确安装wget(图文详解)
查看>>
让你快速了解并掌握如何进行iOS开发技能
查看>>
html绘制三角形(兼容IE6)
查看>>
Maven安装好后包下载的测试命令和配置变量的查看命令:mvn help:system
查看>>
Spring知识点回顾(01)Java Config
查看>>
Git合并指定文件到另一个分支
查看>>
图解内存的工作原理
查看>>
[Oracle]发生 ora-06502 RMAN 在对 catalog DB 同期时出错的调查方法
查看>>
PHP content-type为"application/json"的post过来的数据$_POST接受不到的问题
查看>>
spring security 4.2后出现CouldnotverifytheprovidedCSRFtokenbecauseyoursessionwasnotfound
查看>>
浅谈HTTPS以及Fiddler抓取HTTPS协议
查看>>
Hadoop使用Java进行文件修改删除操作
查看>>
奇虎360Java笔试题
查看>>
git常用命令速查表【转】
查看>>
[转]Python yield 使用浅析
查看>>
Wpf Binding.Path设置
查看>>
jfinal控制器添加多个拦截器
查看>>
跟着百度学PHP[14]-初识PDO数据库抽象层
查看>>
小豆包的学习之旅:机器人定位
查看>>