Re: Hueristic FindWindow()
EDIT > MS has an example that allows using wildcards , How To Get a Window Handle Without Specifying an Exact Title
Otherwise you can try something like this by passing a partial name of the programs title bar caption,
Code:
' Form*.frm
Option Explicit
Private Sub Command1_Click()
Dim i As Long
' search for window titles like,
FindWindowLike "Internet Explorer"
' show results
If UBound(WinMatch) = 0 Then
Debug.Print "No Match Found"
Else
Debug.Print "Matches Found = " & UBound(WinMatch)
Debug.Print "-"
For i = 0 To UBound(WinMatch) - 1
Debug.Print "Title = " & WinMatch(i).Title ' title caption
Debug.Print "Hwnd = " & WinMatch(i).Hwnd ' handle
Debug.Print "-"
Next i
End If
End Sub
Code:
' module*.bas
Option Explicit
Private Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Type WindowInfo
Hwnd As Long
Title As String
End Type
Public WinMatch() As WindowInfo
Public Function FindWindowLike(sPartOfCaption As String) As Long
Dim sCurWinText As String
Dim Hwnd As Long, ret As Long
ReDim WinMatch(0)
Hwnd = GetForegroundWindow
Do Until Hwnd = 0
sCurWinText = Space$(255)
ret = GetWindowText(Hwnd, sCurWinText, 255)
sCurWinText = Left$(sCurWinText, ret)
If InStr(1, LCase(sCurWinText), LCase(sPartOfCaption)) <> 0 Then
WinMatch(UBound(WinMatch)).Title = sCurWinText
WinMatch(UBound(WinMatch)).Hwnd = Hwnd
ReDim Preserve WinMatch(UBound(WinMatch) + 1)
End If
Hwnd = GetWindow(Hwnd, GW_HWNDNEXT)
Loop
End Function
Re: Hueristic FindWindow()
RobDog88 also had a similar project. Might also help you.
Find Window handle by Partial Caption
http://www.vbforums.com/showthread.php?t=316924