VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



FindWindowWild

by Ark (13 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

Find window using full or part of it's caption. Allow wild characters (*,?,[]). For example, using this string :"*Mi??OSoFt In[s-u]ernet*" you can find Microsoft Internet Explorer window.

Inputs
Full or part of window's caption. Wild characters accepted.
Code Returns
Handle of window if find, zero otherwise.

Rate FindWindowWild

'---Bas module code------
Private Declare Function EnumWindows& Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam 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 IsWindowVisible& Lib "user32" (ByVal hwnd As Long)
Private Declare Function GetParent& Lib "user32" (ByVal hwnd As Long)
Dim sPattern As String, hFind As Long
Function EnumWinProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
 Dim k As Long, sName As String
 If IsWindowVisible(hwnd) And GetParent(hwnd) = 0 Then
   sName = Space$(128)
   k = GetWindowText(hwnd, sName, 128)
   If k > 0 Then
    sName = Left$(sName, k)
    If lParam = 0 Then sName = UCase(sName)
    If sName Like sPattern Then
      hFind = hwnd
      EnumWinProc = 0
      Exit Function
    End If
   End If
 End If
 EnumWinProc = 1
End Function
Public Function FindWindowWild(sWild As String, Optional bMatchCase As Boolean = True) As Long
 sPattern = sWild
 If Not bMatchCase Then sPattern = UCase(sPattern)
 EnumWindows AddressOf EnumWinProc, bMatchCase
 FindWindowWild = hFind
End Function

'----Using (Form code)----
Private Sub Command1_Click()
 Debug.Print FindWindowWild("*Mi??OSoFt In[s-u]ernet*", False)
End Sub

Download this snippet    Add to My Saved Code

FindWindowWild Comments

No comments have been posted about FindWindowWild. Why not be the first to post a comment about FindWindowWild.

Post your comment

Subject:
Message:
0/1000 characters