如何使用Excel VBA获取Windows应用程序进程mainwindowtitle和窗口状态属性?
需要使用Excel VBA脚本获取mainwindowtitle或窗口状态属性的帮助吗 在我的windows计算机上,我有两个同名进程运行,例如xyz.exe 其中一个是windows应用程序,另一个是助手或后台进程。我想使用mainwindowtitle或window status属性找出哪个是windows应用程序进程 之所以选择这些属性,是因为后台进程没有mainwindowtitle,并且窗口状态为空。下面是process explorer屏幕截图,显示了这两个进程 使用脚本和应用程序的WMI任务,我可以很容易地找到进程ID,但我无法确定如何获取mainwindowtitle或window status属性如何使用Excel VBA获取Windows应用程序进程mainwindowtitle和窗口状态属性?,vba,excel,win32-process,Vba,Excel,Win32 Process,需要使用Excel VBA脚本获取mainwindowtitle或窗口状态属性的帮助吗 在我的windows计算机上,我有两个同名进程运行,例如xyz.exe 其中一个是windows应用程序,另一个是助手或后台进程。我想使用mainwindowtitle或window status属性找出哪个是windows应用程序进程 之所以选择这些属性,是因为后台进程没有mainwindowtitle,并且窗口状态为空。下面是process explorer屏幕截图,显示了这两个进程 使用脚本和应用程序
Private Sub getP()
strComputer = "."
sExeName = "XYZ.exe"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process
WHERE Name = '" & sExeName & "'", , 48)
For Each objItem In colItems
Debug.Print "ProcessId: " & objItem.ProcessId
Next
End Sub
根据David在评论中提到的内容,尝试以下方法:
Private Const GW_HWNDNEXT = 2
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount 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
Sub ListWins(Optional Title = "*XYZ*", Optional Class = "*")
Dim hWndThis As Long
hWndThis = FindWindow(vbNullString, vbNullString)
While hWndThis
Dim sTitle As String, sClass As String
sTitle = Space$(255)
sTitle = Left$(sTitle, GetWindowText(hWndThis, sTitle, Len(sTitle)))
sClass = Space$(255)
sClass = Left$(sClass, GetClassName(hWndThis, sClass, Len(sClass)))
If sTitle Like Title And sClass Like Class Then
Debug.Print sTitle, sClass
End If
hWndThis = GetWindow(hWndThis, GW_HWNDNEXT)
Wend
End Sub
根据David在评论中提到的内容,尝试以下方法:
Private Const GW_HWNDNEXT = 2
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount 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
Sub ListWins(Optional Title = "*XYZ*", Optional Class = "*")
Dim hWndThis As Long
hWndThis = FindWindow(vbNullString, vbNullString)
While hWndThis
Dim sTitle As String, sClass As String
sTitle = Space$(255)
sTitle = Left$(sTitle, GetWindowText(hWndThis, sTitle, Len(sTitle)))
sClass = Space$(255)
sClass = Left$(sClass, GetClassName(hWndThis, sClass, Len(sClass)))
If sTitle Like Title And sClass Like Class Then
Debug.Print sTitle, sClass
End If
hWndThis = GetWindow(hWndThis, GW_HWNDNEXT)
Wend
End Sub
你知道窗口的标题吗?如果是这样,您可能可以使用WinAPI
FindWindow
函数。可能不起作用,因为标题可以根据打开的文件名进行更改。理论上,您应该能够使用WinAPI函数从ProcessID获取句柄,然后从句柄获取窗口标题。不过我现在不能用这个。你知道窗口的标题吗?如果是这样,您可能可以使用WinAPIFindWindow
函数。可能不起作用,因为标题可以根据打开的文件名进行更改。理论上,您应该能够使用WinAPI函数从ProcessID获取句柄,然后从句柄获取窗口标题。不过我现在没法让它工作。