|
原理将函数地址和参数传给CallWindowProc,由系统进行函数回调。参数多的话可以封装到数组中。
网上有很多详细的介绍,但好像没稳定的实现。自己弄了个通用的方法,用了好几年,简单整理了下,分享给大家。
- '运行速度比接口(I_Compare)形式慢3倍左右,非大量回调的算法类基本无影响
- 'Application.Run慢XX倍
- '排序等算法类建议用接口形式进行回调.
- Declare Function CallBackProc Lib "user32.dll" Alias "CallWindowProcA" (ByVal lpProc As Long, Optional Param1 As Any, Optional Param2 As Any, Optional Param3 As Any, Optional Param4 As Any) As Long
- 'lpProc must be a Sub
- Sub CallBackSub_(ByVal lpProc As Long, Optional ByRef Param1 = 0, Optional ByRef Param2 = 0, Optional ByRef Param3 = 0, Optional ByRef Param4 = 0)
- Call CallBackProc(lpProc, Param1, Param2, Param3, Param4)
- End Sub
- 'lpProc must be a Sub
- Function CallBackFunction_(ByVal lpProc As Long, Optional ByRef Param1 = 0, Optional ByRef Param2 = 0, Optional ByRef Param3 = 0)
- Call CallBackProc(lpProc, CallBackFunction_, Param1, Param2, Param3)
- End Function
- '''==回调函数格式=========================================================================================
- '''所有参数不一定使用但必须声名,参数类型为variant
- Sub SubExample(Param1, Param2, Param3, Param4)
- Debug.Print "SubExample", Param1, Param2, Param3, Param4
- Param1 = 5
- End Sub
- Sub FunctionExample(Ret, Param1, Param2, Param3)
- Ret = Param1 & Param2
- Debug.Print "FunctionExample"
- End Sub
复制代码
|
|