吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2024|回复: 2
收起左侧

[其他原创] EXCEL VBA 项目用来遍历子窗口

[复制链接]
yuxb57905 发表于 2021-7-21 14:18


新建一个名叫UserForm1的窗体创建按钮CommandButton1,黏贴如下代码测试过可以运行,最好再优化一下
Private dwTotalFileNum As Long
Private Const GW_CHILD = 5
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const WM_GETTEXT = 13
Private Const EM_REPLACESEL = 194
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long
Private 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
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Public Function getText(hwnd As Long) As String
    Const WM_GETTEXT = &HD
    Const WM_GETTEXTLENGTH = &HE
    Dim byt() As Byte
    Dim TextLen As Long
    Dim strText As String
    TextLen = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)
    ReDim byt(1 To TextLen) As Byte
    strText = Space(TextLen)
    Call SendMessage(hwnd, WM_GETTEXT, ByVal TextLen + 1, byt(1))
    getText = StrConv(byt, vbUnicode)
End Function


Function GetCkText(hListWnd As Long, szClsName As String)
Dim t As Integer, i As Integer
Dim szCaption As String
szCaption = String$(255, 0)
Select Case szClsName
    Case "Button", "Static"
       i = GetWindowTextLength(hListWnd)
       GetWindowText hListWnd, szCaption, 250
       szCaption = Left$(szCaption, i)
    Case "Edit"
        szCaption = getText(hListWnd)

   ' Case "Static"
         
    Case Else
        
    End Select

GetCkText = szCaption
   
End Function
Private Sub ListAllWndFind(hwnd As Long, szFullPathM As String)
Dim t As Integer
Dim hListWnd As Long
Dim RetVa As Long
Dim szFilePath As String
Dim szFullPath As String
Dim szClsName As String
Dim szCaption As String
szCaption = String$(255, 0)
szClsName = String$(255, 0)
hListWnd = FindWindowEx(hwnd, 0, vbNullString, vbNullString)
   If hListWnd <> 0 Then
     Do
        szFilePath = Str(hListWnd)
        szFullPath = szFullPathM & szFilePath
        dwTotalFileNum = dwTotalFileNum + 1
        szClsName = String$(255, 0)
        RetVa = GetClassName(hListWnd, szClsName, 250)
        't = Len(szClsName)
        szClsName = Left$(szClsName, RetVa)
        
        szCaption = GetCkText(hListWnd, szClsName)
        
        Sheet1.Cells(dwTotalFileNum, 1) = dwTotalFileNum
        Sheet1.Cells(dwTotalFileNum, 2) = szFullPath
        Sheet1.Cells(dwTotalFileNum, 3) = szClsName
        Sheet1.Cells(dwTotalFileNum, 4) = szCaption
        
        If (FindWindowEx(hListWnd, 0, vbNullString, vbNullString)) Then
            ListAllWndFind hListWnd, szFullPath
        End If
     hListWnd = FindWindowEx(hwnd, hListWnd, vbNullString, vbNullString)
     Loop While (hListWnd <> 0)
    Else
     t = MsgBox("没找到主窗口", vbOKOnly)
   End If
End Sub



Private Sub CommandButton1_Click()

Dim t As Integer
Dim hwnd As Long
Dim szFileGPath As String
dwTotalFileNum = 0
hwnd = FindWindow(vbNullString, "计算器")
If hwnd <> 0 Then
szFileGPath = Str(hwnd)
ListAllWndFind hwnd, szFileGPath
Else: t = MsgBox("没找到主窗口", vbOKOnly)
End If
End Sub


Private Sub UserForm_Click()

免费评分

参与人数 2吾爱币 +1 热心值 +2 收起 理由
fengbu401 + 1 + 1 谢谢@Thanks!
hj170520 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

hj170520 发表于 2021-7-21 15:21
能设置下排版吗

6EED0164-66AF-49A9-AFCB-574BA922586F.png
ilpj 发表于 2021-7-22 03:14
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 14:50

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表