I need to find the handles of four windows that have the
same Classname and same Window Caption. Is there a way I
can make a loop with the Findwindow Api to find all four handles?
Printable View
I need to find the handles of four windows that have the
same Classname and same Window Caption. Is there a way I
can make a loop with the Findwindow Api to find all four handles?
You can use the FindWindowEx API, it has a Parameter which specifies which Window Handle to Start Looking From, so you could write a Function to Return an Array of all Windows Matching the Class and/or Caption criteria, ie.
Example of Usage:Code:Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Function GetHwnds(ByVal Class As String, ByVal Caption As String) As Variant
Dim lHwnd() As Long
Dim lCount As Long
Dim lHandle As Long
ReDim lHwnd(0)
Do
lHandle = FindWindowEx(0, lHandle, Class, Caption)
If lHandle Then
ReDim Preserve lHwnd(lCount)
lHwnd(lCount) = lHandle
lCount = lCount + 1
End If
Loop While lHandle
GetHwnds = lHwnd
End Function
Code:Private Sub Command1_Click()
Dim lHwnd As Variant
Dim iIndex As Long
lHwnd = GetHwnds("NotePad", vbNullString)
For iIndex = 0 To UBound(lHwnd)
If lHwnd(iIndex) Then
'Do Whatever with the Window Handle Here..
Debug.Print lHwnd(iIndex)
End If
Next
End Sub
Or you can use EnumWindows API to enumerate all running windows. If you're intersted, then check out Code Snippet I've posted for a similar question.