对应于系统托盘图标的windows可执行文件?

2024-09-28 23:17:06 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一种方法可以使用Python来获取系统托盘(右下角)中所有图标及其相关的进程和可执行文件的列表?在

下面是一个关于如何使用AHK进行此操作的线程:

http://www.autohotkey.com/forum/topic17314.html

但它严重依赖AHK。在

请注意,我对WindowsXP/7感兴趣

谢谢!在


Tags: 方法comhttp可执行文件列表进程htmlwww
2条回答

在我看来,它更多地依赖于windows的api,而不是ahk,ahk可能只是一个包装器,因为您所引用的站点上的参数对于它们的用例来说是愚蠢的,例如hWnd,101,0,0,0,0,它通常暗示windows的api结构类型的东西;)。我非常怀疑你能在这里插入和播放任何东西,因为这是一个有点不寻常的目标,而且对windows来说非常特殊。如果你想自己实现,你很可能会从Tim Golden的网站上得到很多帮助,如果之前有人这么做过,我想应该是他:

{a1}

markhammond和pythonwindows的核心模块也是合理的搜索词,但不知何故,直接用于所有这些的文档仍然相对较少,我想2000年的那本书现在不会有太大帮助。在

否则,您可以拨打ahk可以拨打的任何电话,但这需要阅读MSDN。。。如果您大致知道您希望从AHK脚本中获得什么,那么您的工作量将大大减少。但还是会很烦人。在

如果您想了解一些如何实现windowsapi调用(生成结构、调用函数等)的示例,您可能会发现grayhatpython的早期章节很有用。我发现它比大多数直接来自pythonwindows小组的资料都有用,这些资料无疑只是有点超出我的理解范围。在

祝你好运

这是厚厚的,厚厚的win32东西。我开始用pywin32写这个,但是为了更好地处理结构,我切换到ctypes。注意,我是用32 win XP编写的。您将更改64位的TBBUTTONdef。我不确定UAC会对此做什么(你能在另一个进程中分配内存吗?)。在

import commctrl, win32con
from ctypes import *

# represent the TBBUTTON structure
# note this is 32 bit, 64 bit padds 4 more reserved bytes
class TBBUTTON(Structure):
    _pack_ = 1
    _fields_ = [
        ('iBitmap', c_int),
        ('idCommand', c_int),
        ('fsState', c_ubyte),
        ('fsStyle', c_ubyte),
        ('bReserved', c_ubyte * 2),
        ('dwData', c_ulong),
        ('iString', c_int),
    ]

# get the handle to the sytem tray
hWnd = windll.user32.FindWindowA("Shell_TrayWnd", None)
hWnd = windll.user32.FindWindowExA(hWnd, None, "TrayNotifyWnd", None)
hWnd = windll.user32.FindWindowExA(hWnd, None, "SysPager", None)
hWnd = windll.user32.FindWindowExA(hWnd, None, "ToolbarWindow32", None)

# get the count of icons in the tray
numIcons = windll.user32.SendMessageA(hWnd, commctrl.TB_BUTTONCOUNT, 0, 0)

# allocate memory within the system tray
pid = c_ulong();
windll.user32.GetWindowThreadProcessId(hWnd, byref(pid))
hProcess = windll.kernel32.OpenProcess(win32con.PROCESS_ALL_ACCESS, 0, pid)
lpPointer = windll.kernel32.VirtualAllocEx(hProcess, 0, sizeof(TBBUTTON), win32con.MEM_COMMIT, win32con.PAGE_READWRITE)

# init our tool bar button and a handle to it
tbButton = TBBUTTON()
butHandle = c_int()

for i in range(numIcons):
    # query the button into the memory we allocated
    windll.user32.SendMessageA(hWnd, commctrl.TB_GETBUTTON, i, lpPointer)
    # read the memory into our button struct
    windll.kernel32.ReadProcessMemory(hProcess, lpPointer, addressof(tbButton), 20, None)
    # read the 1st 4 bytes from the dwData into the butHandle var
    # these first 4 bytes contain the handle to the button
    windll.kernel32.ReadProcessMemory(hProcess, tbButton.dwData, addressof(butHandle), 4, None)

    # get the pid that created the button
    butPid = c_ulong()
    windll.user32.GetWindowThreadProcessId(butHandle, byref(butPid))

    # i leave it to you to get the process from the pid
    # that should be trivial...
    print butPid

相关问题 更多 >