EnumThreadWindows Function

Declare Function EnumThreadWindows Lib "user32.dll" (ByVal dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long

Platforms

Description & Usage

EnumThreadWindows enumerates and provides handles to all of the windows owned and controlled by a given thread. (Note that these windows include many windows not visible to the user.) Each time a window is located, the function passes that handle to an application-defined callback function. The function continues doing so until all windows have been enumerated, or until the process has been aborted.

Return Value

If an error occured, the function returns 0 (use GetLastError to get the error code). If successful, the function returns a non-zero value.

Visual Basic-Specific Issues

None.

Parameters

dwThreadId
An identifier to the thread to enumerate the windows of.
lpfn
A pointer to the application-defined callback function EnumThreadWndProc.
lParam
An additional value to pass to the application-defined callback function.

Example

' This code is licensed according to the terms and conditions listed here.

' Display the window title of all windows controlled by the thread
' which the window Form1 is in.  This task is given to the callback function, which
' will receive each handle individually.  Note that if the window has no title bar
' text, it will not be displayed (for clarity's sake).

' *** Place this code in a module.  This is the callback function. ***
' This function displays the title bar text of the window identified by hwnd.
Public Function EnumThreadWndProc (ByVal hwnd As Long, ByVal lParam As Long) As Long
  Dim slength As Long, wintext As String  ' title bar text length and buffer
  Dim retval As Long  ' return value
  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
  If slength > 1  ' if return value refers to non-empty string
    buffer = Space(slength)  ' make room in the buffer
    retval = GetWindowText(hwnd, buffer, slength)  ' get title bar text
    Debug.Print "Window #"; winnum; " : ";  ' display number of enumerated window
    Debug.Print Left(buffer, slength - 1)  ' display title bar text of enumerated window
  End If

  EnumThreadWndProc = 1  ' return value of 1 means continue enumeration
End Function

' *** Place this code wherever you want to enumerate the windows. ***
Dim threadid As Long, processid As Long  ' receive id to thread and process of Form1
Dim retval As Long  ' return value

' Determine the thread which owns the window Form1.
threadid = GetWindowThreadProcessId(Form1.hWnd, processid)
' Use the callback function to list all of the enumerated thrad windows.  Note that lParam
' is set to 0 because we don't need to pass any additional information to the function.
retval = EnumThreadWindows(threadid, AddressOf EnumThreadWndProc, 0)

See Also

EnumChildWindows, EnumWindows

Category

Windows

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Last Modified: August 15, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/e/enumthreadwindows.html