以下代码可以枚举当前系统所有已打开窗口的标题栏名称。可经过测试发现始终枚举不到 IE 和 QQ 的窗口标题,甚至连记事本写字板的窗口标题都枚举不到。貌似有好多窗口都枚举不到,能枚举到的是一大堆在后台运行的程序窗口,可这些对我没用啊。请问以下代码有什么不对的地方吗?EnumWindows不是可以枚举到屏幕上所有的顶层窗口吗,可我的代码为什么枚举不到呢?Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) 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 Function EnumWndProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
    Dim strWndTextBuff As String * 255&
    Dim strWndHeading As String
    
    Call GetWindowText(lhWnd, strWndTextBuff, 255&)
    If (InStr(strWndTextBuff, Chr(0&)) > 0&) Then
        strWndHeading = Left(strWndTextBuff, InStr(strWndTextBuff, Chr(0&)) - 1&)
        Debug.Print strWndHeading
    End If
    EnumWndProc = True
End Function
 
Call EnumWindows(AddressOf EnumWndProc, 0&)

解决方案 »

  1.   

    做了保护的话FindWindow怎么可以发现?
      

  2.   

    我知道QQ的标题是画上去的,但 IE 窗口不是呀,以上的代码我运行测试根本获得不到 IE 窗口标题,就连系统自带的记事本和写字板都获得不到。正因为这样我才觉得很奇怪,怎么会获得不到 IE 的窗口标题呢?难道 IE 窗口是子窗口必须用EnumChildWindows获得吗?
      

  3.   

    真是太不解了,我换成以下的方法就能得到 IE 窗口的标题Private Sub WindowMonitor()
        Dim lnghWnd As Long
        Dim strWndClassBuff As String * 255
        Dim strWndTextBuff As String * 255
        
        lnghWnd = FindWindow("IEFrame", vbNullString)
        Call GetClassName(lnghWnd, strWndClassBuff, 255)
        Call GetWindowText(lnghWnd, strWndTextBuff, 255)    Debug.Print strWndClassBuff
        Debug.Print strWndTextBuff
    End Sub
      

  4.   

    现在我的问题终于解决了!
    说来也怪,我顶楼的代码别人直接复制拿来测试就能正常或取到所有窗口的标题,甚至连QQ的标题也能获取到,但是在我机子上却不行。不过总算问题解决了,以下附上示例代码:
     
    这个可以获得第一个IE窗口内所有页面选项卡的标题:Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 Function EnumWndProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
        Dim lnghWnd As Long
        Dim strWndClassBuff As String * 255
        Dim strWndTextBuff As String * 255
        Dim strWndHeading As String
        
        Call GetClassName(hWnd, strWndClassBuff, Len(strWndClassBuff))
        If (InStr(strWndClassBuff, vbNullChar) > 0&) Then
            If Left(strWndClassBuff, InStr(strWndClassBuff, vbNullChar) - 1&) = "TabWindowClass" Then
                strWndHeading = Left(strWndTextBuff, GetWindowText(hWnd, strWndTextBuff, Len(strWndTextBuff)))
                Debug.Print strWndHeading
            End If
        End If
        EnumWndProc = True
    End Function
     
    Private Sub Command1_Click()
        Dim lnghWnd As Long
        lnghWnd = FindWindow("IEFrame", vbNullString)
        Call EnumChildWindows(lnghWnd, AddressOf EnumWndProc, 0&)
    End Sub 
    而这个示例是使用GetWindow获得所有的窗口标题:Option Explicit
    Private Declare Function GetDesktopWindow Lib "USER32" () As Long
    Private Declare Function GetWindow Lib "USER32" (ByVal hwnd As Long, ByVal wCmd As Long) 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 IsWindowVisible Lib "USER32" (ByVal hwnd As Long) As Long
    Private Const GW_CHILD = &H5
    Private Const GW_HWNDNEXT = &H2Private Sub GetHandle()
       Dim tmp As String
       Dim wHwnd As Long
       Dim lngProcID As Long
       Dim strTitle As String * 255 '用来存储窗口的标题
       
       wHwnd = GetDesktopWindow() '取得桌面窗口句柄
       wHwnd = GetWindow(wHwnd, GW_CHILD) '取得桌面窗口的第一个子窗口
       '通过循环来枚举所有的窗口
       Do While wHwnd <> 0&
            Call GetWindowText(wHwnd, strTitle, Len(strTitle)) '取得下一个窗口的标题
            If Left$(strTitle, 1&) <> vbNullChar Then
                 tmp = Left$(strTitle, InStr(1&, strTitle, vbNullChar))
                 If IsWindowVisible(wHwnd) > 0& Then Form1.List1.AddItem tmp + " - " + Trim(Str(wHwnd))
            End If
            wHwnd = GetWindow(wHwnd, GW_HWNDNEXT) '继续取得下一个窗口
       Loop
    End Sub
     
    Private Sub Command1_Click()
        GetHandle
    End Sub