Results 1 to 4 of 4

Thread: Getting all open window's hWnd

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    11
    FindWindow can be used to find a window's hWnd, but how to get a list of all windows?

  2. #2
    Guest
    Is this what you want?

    Code:
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd 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
    
    Function GetCaption(hwnd)
    hwndLength% = GetWindowTextLength(hwnd)
    hwndTitle$ = String$(hwndLength%, 0)
    a% = GetWindowText(hwnd, hwndTitle$, (hwndLength% + 1))
    
    GetCaption = hwndTitle$
    End Function
    
    Private Sub Command1_Click()
    For i = 1 To 10000
    X = GetCaption(i)
    List1.AddItem X
    Next
    End Sub

  3. #3
    Guest
    Use EnumWindows

    Code for a Module
    Code:
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    
    Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
        Dim Length As Long
        Dim sName As String
        Dim Temp As String
        Static iCount As Integer
        
        iCount = iCount + 1
        Length = GetWindowTextLength(hwnd) + 1
        
        If Length > 1 Then
            sName = Space(Length)
            GetWindowText hwnd, sName, Length
            Debug.Print Left(sName, Length - 1) & " - HWND = " & hwnd
        End If
        
        EnumWindowsProc = 1
    End Function
    Code for a Form with a CommandButton
    Code:
    Private Sub Command1_Click()
        EnumWindows AddressOf EnumWindowsProc, 0
    End Sub

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    'you could list all items in the tasklist in a listbox

    Code:
    'this project uses a list box (list1) and a cmd button (Command1)
    'open a std project
    
    'add a bas module and put this code in it
    '
    Option Explicit
    
    Private Declare Function GetWindow Lib "user32" _
    (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function GetParent Lib "user32" _
    (ByVal hwnd As Long) As Long
    '
    Private Declare Function GetWindowTextLength Lib _
    "user32" Alias "GetWindowTextLengthA" (ByVal hwnd 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
    
    Const GW_HWNDFIRST = 0
    Const GW_HWNDNEXT = 2
    
    Public Sub LoadTaskList()
    Dim CurrWnd As Long
    Dim Length As Long
    Dim TaskName As String
    Dim Parent As Long
    
    Form1.List1.Clear
    CurrWnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)
    
    While CurrWnd <> 0
    Parent = GetParent(CurrWnd)
    Length = GetWindowTextLength(CurrWnd)
    TaskName = Space$(Length + 1)
    Length = GetWindowText(CurrWnd, TaskName, Length + 1)
    TaskName = Left$(TaskName, Len(TaskName) - 1)
    
    If Length > 0 Then
    If TaskName <> Form1.Caption Then
    Form1.List1.AddItem TaskName
    End If
    End If
    CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
    DoEvents
    
        Wend
        
    End Sub
    '
    '=========================================================
    '
    'in the click event of the command button
    '
    '	
        LoadTaskList
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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