VB6、按键精灵中用循环和栈实现递归算法

标题: 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
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容