標(biāo)題: VB6蒸甜、按鍵精靈中用循環(huán)和棧實現(xiàn)遞歸算法
作者: 神夢無痕(QQ:1042207232)
鏈接:
http://www.reibang.com/p/bcc00fff0894
版權(quán): 本人所有文章却嗡,都遵守“
署名-非商業(yè)性使用-相同方式共享 2.5 中國大陸”協(xié)議條款闰渔。
'遍歷當(dāng)前句柄的所有子句柄
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
'用循環(huán)和棧代替遞歸:
棧_數(shù)組 = 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 棧_數(shù)組(i) : 棧_數(shù)組(i) = Array(Hwnd, HwndIE)
Hwnd = HwndIE : HwndIE = 0
Else
If i = -1 Then Exit Do
'------[出棧]------
Hwnd = 棧_數(shù)組(i)(0) : HwndIE = 棧_數(shù)組(i)(1) : i = i - 1
End If
Loop
棧_數(shù)組 = ""
End Function
VB6 中需要加上下面的語句,否則報錯
Function TracePrint(Str)
debug.Print Str
End Function