如何使用 vba 在 windowapi 中使用 findwindow 函数定位 window?

How to locate the window using findwindow function in windowapi using vba?

我目前正在尝试找到一种使用 Findwindow 函数检查 window 是否打开的方法。如果我知道 window 的全名,我就能找到 window。在下面的代码中,我知道 window 的名称是 "win32api - Notepad" 所以我可以轻松找到 window 但是我想知道是否可以识别 window如果我只知道零件名称,例如 "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

一种方法是使用 EnumWindows API 函数。由于它通过回调函数运行,因此您需要将条件和结果缓存在调用函数范围之外的某个地方:

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

下面的代码对我有用。刚刚声明了 IsWindowVisible 函数并将 Microsoft 脚本运行时库添加到我的项目中。

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