zoukankan      html  css  js  c++  java
  • [转]在C#中使用API回调函数的方法

    在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类的窗口后,停止枚举。

  • 相关阅读:
    接口自动化架构-获取用例
    Windows性能监控工具Perfmon使用指南
    接口自动化架构1-setting
    多进程
    线程锁、守护线程
    多线程
    xlrd模块
    封装写日志的类
    封装redis
    继承
  • 原文地址:https://www.cnblogs.com/zzyhost/p/3585655.html
Copyright © 2011-2022 走看看