Results 1 to 2 of 2

Thread: Enumerating Windows

  1. #1

    Thread Starter
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334

    Question

    How do I use EnumWindows & EnumWindowsProc?

    I neec to find out if a DOS window that opens after being shelled from VB (the DOS window is MakeCab.EXE) has finished in order to 'Kill' it's text input file.

    When I use GetWindow I get the handle of the calling application and not the DOS window.

    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    You can use this small routine I wrote a while back to see all windows that are running with their ClassNames, Captions and hWnds. Start a new project. Add a ListView and a module to your project. Copy this code to a module:

    Module Code:
    Code:
    Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam 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 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
    
    Public Function EnumWinProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
        Dim strClassName As String
        Dim strCaption As String
        Dim lCaptionLength As Long
        Dim lRet As Long
        Dim lstItem As ListItem
        
        
        strClassName = Space(255)
        lRet = GetClassName(hwnd, strClassName, Len(strClassName))
        Set lstItem = Form1.ListView1.ListItems.Add(, , hwnd)
        If lRet Then
            lstItem.SubItems(1) = Left(strClassName, lRet)
        End If
        
        lCaptionLength = GetWindowTextLength(hwnd)
        strCaption = Space(lCaptionLength)
        lRet = GetWindowText(hwnd, strCaption, lCaptionLength)
        If lRet Then
            lstItem.SubItems(2) = Left(strCaption, lRet)
        End If
        
        EnumWinProc = 1
    End Function
    Then copy this code to your Form:

    Form Code:
    Code:
    Private Sub Form_Load()
        Dim hdrHeader As ColumnHeader
        
        With ListView1
            .View = lvwReport
            Set hdrHeader = .ColumnHeaders.Add(, , "hWnd")
            Set hdrHeader = .ColumnHeaders.Add(, , "Class", 2000)
            Set hdrHeader = .ColumnHeaders.Add(, , "Caption", 4000)
            .FullRowSelect = True
        End With
        
        Call EnumWindows(AddressOf EnumWinProc, 0)
    End Sub
    Running the project will list all running windows (including hidden) with their hWnd, Class and Caption.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width