先看最终代码:
' 枚举窗口函数
Declare Function EnumWindows lib "user32.dll" Alias "EnumWindows" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
' 回调函数
Callback Function EnumWindowProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
TracePrint "窗口句柄:" & hWnd & vbtab & vbtab & "窗口标题:" & Window_GetCaption(hWnd)
return true
End Function
' 执行入口函数
Function Main() As Long
EnumWindows(CodePtr(EnumWindowProc), 0)
End Function使用 CodePtr 函数可以获取对应函数的回调指针,这个指针可以作为回调函数使用。
本例程使用了 Window_GetCaption 命令,需要在工程中引入 xLib 扩展库。
果壳精灵 Ver 1.0 Alpha 5 及以上版本可以在创建工程的时候,在【根据模板创建】分类下找到【枚举窗口(API)】。
根据这个模板创建的工程即可获取本范例开箱即用的代码。