vba - 如何使用 Excel VBA 获取 Windows 应用程序进程主窗口标题和窗口状态属性?

标签 vba excel win32-process

使用 获取主窗口标题或窗口状态属性需要帮助Excel VBA 脚本 ?

在我的 Windows 机器上,我有两个以相同名称运行的进程,例如xyz.exe。

其中一个具有 Windows 应用程序,另一个是帮助程序或后台进程。我想找出哪个是使用 mainwindowtitle 或窗口状态属性的 windows 应用程序进程。

我之所以选择这些属性,是因为后台进程没有主窗口标题且窗口状态为空。下面是显示这两个进程的进程资源管理器屏幕截图。

enter image description here

将 WMI 任务用于脚本和应用程序我可以轻松找到进程 ID,但我无法弄清楚如何获取 mainwindowtitle 或窗口状态属性。

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

最佳答案

根据大卫在评论中提到的内容,试试这个:

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

关于vba - 如何使用 Excel VBA 获取 Windows 应用程序进程主窗口标题和窗口状态属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41511615/

相关文章:

excel - 如何从 VLookup 返回值中保留日期格式?

c# - 提高长时间操作的性能

vba - 无法运行宏 该工作簿中的宏可能不可用,或者所有宏都可能被禁用

excel - VBA运行时错误 '380' : A script engine for the specified language can not be created

Excel VBA 编辑器自动取消大写属性

mysql - 尝试在 AutoCAD 和 mySQL 之间创建链接以自动完成其他属性

excel - 如果将重复记录插入表中, Access 不会引发错误

c++ - 从使用 glfw 创建的窗口中删除最大化按钮

windows - 确定 Win32 函数的调用进程

vba - 上传文件到网站