EnumWindows Function

Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Platforms

Description & Usage

EnumWindows enumerates and provides handles to all of the currently open top-level windows. This function will ignore child windows. (Note that the top-level 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

lpEnumFunc
A pointer to the application-defined callback function EnumWindowsProc.
lParam
An additional value to pass to the application-defined callback function.

Example:

' Display the title bar text of all top-level windows.  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 EnumWindowsProc (ByVal hwnd As Long, ByVal lParam As Long) As Long
  Dim slength As Long, buffer 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

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

' *** Place this code wherever you want to enumerate the windows. ***
Dim retval As Long  ' return value

' Use the above callback function to list all of the enumerated windows.  Note that lParam is
' set to 0 because we don't need to pass any additional information to the function.
retval = EnumWindows(AddressOf EnumWindowsProc, 0)

See Also

EnumChildWindows, EnumThreadWindows

Category

Windows

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


Last Modified: January 16, 2000
This page is copyright © 2000 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/enumwindows.html