gpt4 book ai didi

vba - 如何使用vba在windowapi中使用findwindow函数定位窗口?

转载 作者:行者123 更新时间:2023-12-02 05:40:42 27 4
gpt4 key购买 nike

我目前正在尝试找到一种方法来使用 Findwindow 函数检查窗口是否打开。如果我知道窗口的完整名称,我就能找到该窗口。在下面的代码中,我知道窗口的名称是“win32api - Notepad”,因此我可以轻松找到该窗口,但是我想知道如果我只知道“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

关于vba - 如何使用vba在windowapi中使用findwindow函数定位窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34440961/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com