VBA-获取任务栏应用程序

多杰

我在网上找到了一个代码:

Public Sub showProcesses()
    Dim W As Object
    Dim ProcessQuery As String
    Dim processes As Object
    Dim process As Object
    Set W = GetObject("winmgmts:")
    ProcessQuery = "SELECT * FROM win32_process"
    Set processes = W.execquery(ProcessQuery)
    For Each process In processes
        MsgBox process.Name
        MsgBox process.Description
    Next
    Set W = Nothing
    Set processes = Nothing
    Set process = Nothing
End Sub

它打印出所有活动进程的名称和描述。

例子:

执行文件

EXCEL.EXE

但是,任务管理器中还有另一个选项卡,用于显示应用程序(与任务栏上的应用程序相同)。我想创建一个读取其名称的程序。

例子:

在我的任务栏中,Chrome,Outlook和Excel是打开的应用程序,因此我希望我的程序打印出来:

Microsoft Excel-书1

收件箱[email protected]

VBA-获取任务栏应用程序(<-Chrome)

垃圾桶

这应该为您指明正确的方向。我能够对其进行测试,并在立即窗口(ctrl-G)中查看结果。您将需要编辑以在单元格中显示。http://access.mvps.org/access/api/api0013.htm

更新,添加了我的原始作者代码的编辑版本以回答问题

Private Declare PtrSafe Function apiGetClassName Lib "user32" Alias _
                "GetClassNameA" (ByVal Hwnd As Long, _
                ByVal lpClassname As String, _
                ByVal nMaxCount As Long) As Long
Private Declare PtrSafe Function apiGetDesktopWindow Lib "user32" Alias _
                "GetDesktopWindow" () As Long
Private Declare PtrSafe Function apiGetWindow Lib "user32" Alias _
                "GetWindow" (ByVal Hwnd As Long, _
                ByVal wCmd As Long) As Long
Private Declare PtrSafe Function apiGetWindowLong Lib "user32" Alias _
                "GetWindowLongA" (ByVal Hwnd As Long, ByVal _
                nIndex As Long) As Long
Private Declare PtrSafe Function apiGetWindowText Lib "user32" Alias _
                "GetWindowTextA" (ByVal Hwnd As Long, ByVal _
                lpString As String, ByVal aint As Long) As Long
Private Const mcGWCHILD = 5
Private Const mcGWHWNDNEXT = 2
Private Const mcGWLSTYLE = (-16)
Private Const mcWSVISIBLE = &H10000000
Private Const mconMAXLEN = 255

Function fEnumWindows()
Dim lngx As Long, lngLen As Long
Dim lngStyle As Long, strCaption As String

    lngx = apiGetDesktopWindow()
    'Return the first child to Desktop
    lngx = apiGetWindow(lngx, mcGWCHILD)

    Do While Not lngx = 0
        strCaption = fGetCaption(lngx)
        If Len(strCaption) > 0 Then
            lngStyle = apiGetWindowLong(lngx, mcGWLSTYLE)
            'enum visible windows only
            If lngStyle And mcWSVISIBLE Then
                 ActiveCell.Value = fGetCaption(lngx)
                 ActiveCell.Offset(1, 0).Activate
            End If
        End If
        lngx = apiGetWindow(lngx, mcGWHWNDNEXT)
    Loop
End Function


Private Function fGetCaption(Hwnd As Long) As String
    Dim strBuffer As String
    Dim intCount As Integer

    strBuffer = String$(mconMAXLEN - 1, 0)
    intCount = apiGetWindowText(Hwnd, strBuffer, mconMAXLEN)
    If intCount > 0 Then
        fGetCaption = Left$(strBuffer, intCount)
    End If
End Function


Sub test()
Range("A1").Activate
 Call fEnumWindows
End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在我的任务栏上获取“获取Windows 10应用程序”图标

从任务栏隐藏UWP应用程序

Flash应用程序的任务栏按钮?

如何从Windows任务栏隐藏应用程序?

Windows任务栏-阻止某些应用程序分组?

应用程序的自定义任务栏菜单

如何杀死系统任务栏图标应用程序?

我的应用程序的任务栏问题

XFCE:从任务栏隐藏应用程序

启动应用程序时ClickOnce应用程序在任务栏中的图标消失

为什么应用程序无法在任务栏上显示其任务栏图标?

使我的wpf应用程序全屏显示(覆盖任务栏和窗口标题栏)

如何设置Java应用程序的自定义框架和任务栏图标?

系统托盘/任务栏图标/带有通用应用程序的通知图标

禁用KDE Chat应用程序和系统任务栏图标

WPF 应用程序不更新任务栏图标

是否有可能给AWT应用程序在Windows 10时整任务栏图标

如何薄荷19肉桂任务栏应用程序删除分组

从任务栏/扩展坞中删除/隐藏Java应用程序

wxWidgets windows 10 应用程序的任务栏进度条

如何将应用程序从通知区域返回到任务栏

如何防止Java应用程序创建任务栏图标?

更改KDE任务栏上非活动应用程序的字体颜色

将Java应用程序固定到Windows 7任务栏

客户应用程序图标未显示在任务栏中-为什么?

固定在Windows任务栏上的应用程序上的标题匹配

Swing Java中具有多框架应用程序的任务栏图标

从任务栏运行应用程序时的字体渲染问题

从任务栏隐藏应用程序时未收到PostMessage