VB中callbacke function 如下:
Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim slength As Long, WndTitle As String ' title bar text length and buffer
    Dim retval As Long ' return value
    Dim WClass As String * 50
    Static winnum As Integer ' counter keeps track of how many windows have been enumerated
    winnum = winnum + 1 ' one more window enumerated....
    
    slength = GetWindowTextLength(hwnd) + 1 ' get length of title bar text
    WndTitle = Space(slength) ' make room in the buffer
    retval = GetWindowText(hwnd, WndTitle, slength) ' get title bar text    GetClassName hwnd, WClass, 50
    EnumChildProc = 1 ' return value of 1 means continue enumeration
End Function结果列表在excel中,窗口句柄为66622的类可以找到,但是Title却为空
ChildWndHnd ChildWndClass ChildWndTitle
66624 TCoolBar
66626 TToolBar
66620 TCoolBar
66622 TToolBar
66618 TPanel但是为什么SPY++中却可以找到:

解决方案 »

  1.   

    retval = GetWindowText(hwnd, WndTitle, slength)检查retval的值,看看函数执行的结果是什么.API的调用都会有返回值,都会告诉你发生了些什么.
      

  2.   

    以上代码看不出hwnd是如何得到的
      

  3.   

    hwnd应该是正确的.这个过程是enumchildwindow的回调.
      

  4.   

    '标准模块
    Option ExplicitPrivate Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Public Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
    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 LongPublic Function EnumChildProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
        Dim slength As Long, WndTitle As String ' title bar text length and buffer
        Dim retval As Long ' return value
        Dim WClass As String * 256
        'Static winnum As Integer ' counter keeps track of how many windows have been enumerated
        'winnum = winnum + 1 ' one more window enumerated....
        
        slength = GetWindowTextLength(hWnd) + 1 ' get length of title bar text
        WndTitle = Space(slength) ' make room in the buffer
        retval = GetWindowText(hWnd, WndTitle, slength) ' get title bar text
        
        Call GetClassName(hWnd, WClass, 256)
           
        WClass = Left(WClass, InStr(WClass, Chr(0)) - 1)
             
        Form1.List1.AddItem hWnd & " 标题:" & WndTitle
        Form1.List2.AddItem hWnd & " 类名:" & WClass
        
        EnumChildProc = 1 ' return value of 1 means continue enumeration
    End Function'窗体模块
    Option ExplicitPrivate Sub Command1_Click()
            EnumWindows AddressOf EnumChildProc, ByVal 0&
    End Sub
      

  5.   

    我的代码就是参考上面的代码的, 执行的的结果就是界面上看得见的Caption都可以得到,但是对于WndID=66622的TToolBar是看不见的,但是SPY++咋就可以得到
      

  6.   

    EnumWindows 只能枚举顶级窗口,要枚举子窗口的话要用:EnumChildWindows