Click to Rate and Give Feedback
MSDN
MSDN Library
User Interface
Windowing
Windows
Windows Reference
Functions
 EnumWindows Function
EnumWindows Function

The EnumWindows function enumerates all top-level windows on the screen by passing the handle to each window, in turn, to an application-defined callback function. EnumWindows continues until the last top-level window is enumerated or the callback function returns FALSE.

Syntax

BOOL EnumWindows(      
    WNDENUMPROC lpEnumFunc,     LPARAM lParam );

Parameters

lpEnumFunc
[in] Pointer to an application-defined callback function. For more information, see EnumWindowsProc.
lParam
[in] Specifies an application-defined value to be passed to the callback function.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

If EnumWindowsProc returns zero, the return value is also zero. In this case, the callback function should call SetLastError to obtain a meaningful error code to be returned to the caller of EnumWindows.


Remarks

The EnumWindows function does not enumerate child windows, with the exception of a few top-level windows owned by the system that have the WS_CHILD style.

This function is more reliable than calling the GetWindow function in a loop. An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed.

Function Information

Minimum DLL Versionuser32.dll
HeaderDeclared in Winuser.h, include Windows.h
Import libraryUser32.lib
Minimum operating systems Windows 95, Windows NT 3.1
UnicodeImplemented as Unicode version.

See Also

Windows Overview, EnumChildWindows, EnumWindowsProc, GetWindow
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content      
Possible VB9 declaration      Dzonny ... That Asian Guy 那個亞洲人   |   Edit   |  
Friend Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32

Example:

''' <summary>Gets all the top-level windows</summary>
''' <returns>List of all top-level windows</returns>
''' <exception cref="API.Win32APIException">An error occured</exception>
Public Shared ReadOnly Property TopLevelWindows() As IReadOnlyList(Of Win32Window)
Get
Dim List As New List(Of Win32Window)
If API.EnumWindows(New API.EnumWindowsProc(Function(hWnd As Integer, lParam As Integer) AddToList(List, New Win32Window(hWnd))), 0) Then
Return New ReadOnlyListAdapter(Of Win32Window)(List)
Else
Dim ex As New API.Win32APIException
If ex.NativeErrorCode <> 0 Then
Throw ex
Else
Return New ReadOnlyListAdapter(Of Win32Window)(List)
End If
End If
End Get
End Property
''' <summary>Adds <paramref name="item"/> to <paramref name="List"/> and returns true</summary>
''' <param name="List"><see cref="List(Of T)"/> to add item to</param>
''' <param name="item">Item to be added</param>
''' <typeparam name="T">Type of <paramref name="item"/></typeparam>
''' <returns>True</returns>
Private Shared Function AddToList(Of T)(ByVal List As List(Of T), ByVal item As T) As Boolean
List.Add(item)
Return True
End Function
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker