枚举所有窗口句柄问题,求教
 悬赏分:50 - 离问题结束还有 14 天 23 小时
我需要实现:枚举所有存在的窗口,发现窗口标题[含有]“123”,就把该窗口的句柄输出到Text1中,但是不知道代码某处出了错误,一直取不出,请高手帮我修改一下 O(∩_∩)O谢谢 小生有礼~ 新手求教,希望能给出修改后的源码 O(∩_∩)O谢谢 
Private Sub Command2_Click() Dim l As Long l = EnumWindows(AddressOf EnumWindowsProc, 0) Text1.Text = xx End Sub 
以下代码我存在独立的Module里的: 
Module: Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long 
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long 
Dim RetVal As Long 
Dim dd As String * 255 
Dim a As String a = "123" RetVal = GetWindowText(hwnd, dd, 255) 
If c = InStr(dd, a) Then 
xx = dd 
Else 
End If 
End Function

解决方案 »

  1.   

    EnumWindows只能列举顶级窗体,对于非顶级窗体,需要继续使用EnumChildWindows
      

  2.   

    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long 
    Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long 
    Dim RetVal As Long 
    Dim dd As String * 255 
    Dim a As String a = "123" RetVal = GetWindowText(hwnd, dd, 255) 
    If c = InStr(dd, a) Then 
    xx = dd 
    Else 
    End If 
    EnumWindowsProc=1
    End Function
      

  3.   

    chenhui530 的意思我明白了  谢谢 chenhui530 大哥   但是代码还是有问题   我点了
    Command2之后 还是没办法把xx输出到TEXT1   再次求教  
      

  4.   

    Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long 
    Dim RetVal As Long 
    Dim dd As String * 255 
    Dim a As String a = "123" RetVal = GetWindowText(hwnd, dd, 255) 
    If c = InStr(dd, a) Then 
    form1.text1.text = dd 
    Else 
    End If 
    EnumWindowsProc=1 
    End Function
      

  5.   

    再次郁闷  按照chenhui530 大哥的写发 无论我把a="123"改成a="321"还是其他 点了Command2后 form1.text1.text一直是显示“Default IME“    而在我的想法中  我需要form1.text1.text = dd 显示的是窗口标题[含有]“123”的完整窗口标题     form1.text1.text = hwnd 显示的是窗口标题[含有]“123”的窗口句柄    我是超级新手,请大哥们包含,再给我修改一次代码   O(∩_∩)O谢谢~~~~~
      

  6.   

    Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long 
    Dim RetVal As Long 
    Dim dd As String * 255 
    Dim a As String a = "123" RetVal = GetWindowText(hwnd, dd, 255) 
    If 0<> InStr(dd, a) Then 
    form1.text1.text = form1.text1.text & vbcrlf & dd 
    Else 
    End If 
    EnumWindowsProc=1 
    End Function
      

  7.   

    Form1窗体模块:Option ExplicitPrivate Sub Command2_Click()        Dim l As Boolean
            
            Text1.Text = ""        l = EnumWindows(AddressOf EnumWindowsProc, ByVal 0&)        'Text1.Text = xxEnd Sub
    标准模块:Option ExplicitPublic 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 EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
             Dim RetVal As Long
             Dim CaptionLength As Long
             Dim WindowCaption As String
             Dim a As String
            
             a = "123"
             
             CaptionLength = GetWindowTextLength(hwnd) '取得顶级窗口标题长度
             WindowCaption = Space(CaptionLength) '设置窗口标题长度
             
             RetVal = GetWindowText(hwnd, WindowCaption, CaptionLength + 1)
             
             'WindowCaption = Left(WindowCaption, InStr(WindowCaption, Chr(0)) - 1)  '去掉尾部空格
             
             Debug.Print WindowCaption
             
             If InStr(WindowCaption, a) > 0 Then
                Form1.Text1.Text = Form1.Text1.Text & WindowCaption & vbCrLf
             Else
                '''''''''''''''''
             End If
             EnumWindowsProc = 1 'True
    End Function