How to find a window using findwindow function in windowapi using vba?

I'm currently trying to find a way to check if a window is open or not using the Findwindow function. I can find the window if I know the whole name of the window. In the code below, I know that the window name is "win32api - Notepad", so I can easily find the window, but I want to know if the window can be identified if I only know the name of the part, for example "win32 *".

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Sub runapplication() hwnd = FindWindow(vbNullString, "win32api - Notepad") MsgBox (hwnd) End Sub 
+6
source share
2 answers

One way to do this is with the EnumWindows API function. Since it works through a callback function, you need to cache both criteria and results somewhere that have scope outside the call function:

 Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, _ ByVal param 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 Const MAX_LEN = 260 Public results As Dictionary Public criteria As String Public Sub Example() criteria = "win32*" Set results = New Dictionary Call EnumWindows(AddressOf EnumWindowCallback, &H0) Dim result As Variant For Each result In results.Keys Debug.Print result & " - " & results(result) Next result End Sub Public Function EnumWindowCallback(ByVal hwnd As Long, ByVal param As Long) As Long Dim retValue As Long Dim buffer As String If IsWindowVisible(hwnd) Then buffer = Space$(MAX_LEN) retValue = GetWindowText(hwnd, buffer, Len(buffer)) If retValue Then If buffer Like criteria Then results.Add hwnd, Left$(buffer, retValue) End If End If End If EnumWindowCallback = 1 End Function 
+4
source

The following code worked for me. The IsWindowVisible function is simply declared and the Microsoft scripting runtime library is added for my project.

 Public Declare Function EnumWindows Lib "User32" (ByVal lpEnumFunc As Long, _ ByVal param 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 IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Long Public Const MAX_LEN = 260 Public results As Dictionary Public criteria As String Public Sub Example() criteria = "win32" Set results = New Dictionary Call EnumWindows(AddressOf EnumWindowCallback, &H0) Dim result As Variant For Each result In results.Keys Debug.Print result & " - " & results(result) Next result End Sub Public Function EnumWindowCallback(ByVal hWnd As Long, ByVal param As Long) As Long Dim retValue As Long Dim buffer As String If IsWindowVisible(hWnd) Then buffer = Space$(MAX_LEN) retValue = GetWindowText(hWnd, buffer, Len(buffer)) If retValue Then If InStr(1, buffer, criteria, vbTextCompare) > 0 Then results.Add hWnd, Left$(buffer, retValue) End If End If End If EnumWindowCallback = 1 End Function 
+2
source

All Articles