VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Enumerating Windows Using the API

by Angsuman Banerji (23 Submissions)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 4th January 2008
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Enumerating Windows Using the API

API Declarations


ByVal lpEnumFunc As Long, _
ByVal lparam As Long _
) As Long

Public Declare Function IsWindowVisible Lib "user32" ( _
ByVal hwnd As Long) As Long

Public Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" ( _
ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long _
) As Long

Public Declare Function GetWindowTextLength Lib "user32" _
Alias "GetWindowTextLengthA" ( _
ByVal hwnd As Long _
) As Long

Public Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" ( _
ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long _
) As Long


Rate Enumerating Windows Using the API




Private m_cSink As IEnumWindowsSink

Private Function EnumWindowsProc( _
    ByVal hwnd As Long, _
    ByVal lparam As Long _
  ) As Long
Dim bStop As Boolean

    bStop = False
    m_cSink.EnumWindow hwnd, bStop
    If (bStop) Then
        EnumWindowsProc = 0
    Else
        EnumWindowsProc = 1
    End If
End Function

Public Function EnumerateWindows( _
        ByRef cSink As IEnumWindowsSink _
    ) As Boolean
    If Not (m_cSink Is Nothing) Then 
        Exit Function
    End If

    Set m_cSink = cSink
    EnumWindows AddressOf EnumWindowsProc, cSink.Identifier
    Set m_cSink = Nothing

End Function

'You are now in a position to use this from any form or class. By telling the
'form/class to implement the IEnumWindowsSink methods, VB will automatically 
'put the EnumWindow sub and Identifier Property Get into the code, requiring 
'you to code them. Here is a sample showing how to find all the Windows on the 
'system from a form. The items are placed into a ListView control called 
'lvwWindows with 4 columns: 

Implements IEnumWindowsSink

Private Sub IEnumWindowsSink_EnumWindow( _
    ByVal hwnd As Long, _
    bStop As Boolean _
    )
Dim itmX As ListItem
    Set itmX = lvwWindows.ListItems.Add(, , WindowTitle(hwnd))
    itmX.SubItems(1) = ClassName(hwnd)
    itmX.SubItems(2) = hwnd
    itmX.SubItems(3) = IsWindowVisible(hwnd)

End Sub

Private Property Get IEnumWindowsSink_Identifier() As Long
    IEnumWindowsSink_Identifier = Me.hwnd
End Property

'The definitions of the functions to get a Window's title, class and 
'visibility from a hWnd are as follows: 

Public Function WindowTitle(ByVal lHwnd As Long) As String
Dim lLen As Long
Dim sBuf As String

    ' Get the Window Title:
    lLen = GetWindowTextLength(lHwnd)
    If (lLen > 0) Then
        sBuf = String$(lLen + 1, 0)
        lLen = GetWindowText(lHwnd, sBuf, lLen + 1)
        WindowTitle = Left$(sBuf, lLen)
    End If
    
End Function

Public Function ClassName(ByVal lHwnd As Long) As String
Dim lLen As Long
Dim sBuf As String
    lLen = 260
    sBuf = String$(lLen, 0)
    lLen = GetClassName(lHwnd, sBuf, lLen)
    If (lLen <> 0) Then
        ClassName = Left$(sBuf, lLen)
    End If
End Function


Download this snippet    Add to My Saved Code

Enumerating Windows Using the API Comments

No comments have been posted about Enumerating Windows Using the API. Why not be the first to post a comment about Enumerating Windows Using the API.

Post your comment

Subject:
Message:
0/1000 characters