|
[楼主] 在C#中使用API回调函数的方法 就以EnumChildWindows和EnumChildProc为例子: 首先要声明EnumChildProc 为一个回调函数 public delegate bool EnumChildProc(int hwnd, IntPtr lParam); delegate为C#中的回调类型,相当于C++里面的CALLBACK,这样就可以在下面声明EnumChildWindows的时候在参数中使用EnumChildProc来作为一个类型。 声明调用user32.dll中的EnumChildWindows,如下: [DllImport("user32.dll", EntryPoint = "EnumChildWindows")] public static extern bool EnumChildWindows(int hwndParent, EnumChildProc EnumFunc, IntPtr lParam); 定义一个和EnumChildProc返回值和参数一样的函数: bool EnumCP(int hwnd,IntPtr lParam) { System.Text.StringBuilder sbClassName = new StringBuilder(255); GetClassName(hwnd, sbClassName, 255); if ("Static" == sbClassName.ToString()) { return false; } return true; } 这时就可以使用EnumChildWindows(hwnd, EnumCP, IntPtr.Zero); 当EnumCP返回true的时候,EnumChildWindow继续进行枚举窗口。当EnumCP返回false的时候,EnumChildWindow停止进行枚举窗口。 上面代码实行的是当找到一个Static类的窗口后,停止枚举。 |
|