标题: VB6、按键精灵中用循环和栈实现递归算法
作者: 神梦无痕(QQ:1042207232)
链接:
https://www.jianshu.com/p/bcc00fff0894
版权: 本人所有文章,都遵守“
署名-非商业性使用-相同方式共享 2.5 中国大陆”协议条款。
'遍历当前句柄的所有子句柄
Call 遍历所有子句柄(GetForegroundWindow())
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Function 遍历所有子句柄(Hwnd)
Dim i, HwndIE
'用循环和栈代替递归:
栈_数组 = Array() : i = -1 : HwndIE = 0
Do
HwndIE = FindWindowEx(Hwnd, HwndIE, vbNullString, vbNullString)
If HwndIE > 0 Then
sClass = Space(50)
GetClassName HwndIE, sClass, Len(sClass) : sClass = Left(sClass, Len(Trim(sClass)) - 1)
sText = Space(255)
GetWindowText HwndIE, sText, Len(sText) : sText = Left(sText, Len(Trim(sText)) - 1)
TracePrint HwndIE & "|" & sClass & "|" & sText
'------[入栈]------
i = i + 1 : Redim Preserve 栈_数组(i) : 栈_数组(i) = Array(Hwnd, HwndIE)
Hwnd = HwndIE : HwndIE = 0
Else
If i = -1 Then Exit Do
'------[出栈]------
Hwnd = 栈_数组(i)(0) : HwndIE = 栈_数组(i)(1) : i = i - 1
End If
Loop
栈_数组 = ""
End Function
VB6 中需要加上下面的语句,否则报错
Function TracePrint(Str)
debug.Print Str
End Function
网友评论