欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python 操作隐藏删除 windows10 任务栏图标(Tray)(tools bar)

程序员文章站 2022-07-02 20:30:41
...

由于最近用 Python 开发一个程序, 程序中涉及隐藏或删除任务栏中某一个图标,如图:

先下载 :pip install pywin32

# python3.8
# -*- coding: utf-8 -*-
# @CreateTime    : 2021/11/25 21:53
# @Author  : JingJunKe
# @File    : 图标测试6.py
# @Software: PyCharm

# shellTray = win32gui.FindWindow("WeChatMainWndForPC", None)
# print(shellTray)
# win32gui.ShowWindow(shellTray, 1)
# exit()


from ctypes import *

import commctrl
import win32api
import win32con
import win32gui


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),
    ]


class TEXT(Structure):
    _fields_ = [
        ('value', c_char * 128),
        ('raw', c_char * 128)
    ]


hWnd = win32gui.FindWindow("Shell_TrayWnd", None)  # 获取任务栏句柄
hWnd = win32gui.FindWindowEx(hWnd, None, "TrayNotifyWnd", None)  # 获取任务栏右下角区域句柄
hWnd = win32gui.FindWindowEx(hWnd, None, "SysPager", None)  # 获取通知区域句柄
hWnd = win32gui.FindWindowEx(hWnd, None, "ToolbarWindow32", None)  # 获取通知区域句柄

# get the count of icons in the tray
numIcons = win32gui.SendMessage(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)
rProcess = windll.kernel32.OpenProcess(win32con.PROCESS_ALL_ACCESS, 0, pid)
textPointer = windll.kernel32.VirtualAllocEx(rProcess, 0, sizeof(TEXT), win32con.MEM_COMMIT, win32con.PAGE_READWRITE)

tbButton = TBBUTTON()
text = TEXT()
for i in range(numIcons):
    win32api.SendMessage(hWnd, commctrl.TB_GETBUTTON, i, lpPointer)  # 通过微软接口获取按钮信息
    windll.kernel32.ReadProcessMemory(hProcess, lpPointer, addressof(tbButton), sizeof(tbButton), None)  # 写入内存
    win32gui.SendMessage(hWnd, commctrl.TB_GETBUTTONTEXTA, tbButton.idCommand, textPointer)  # 获取按钮TEXT
    windll.kernel32.ReadProcessMemory(rProcess, textPointer, addressof(text), sizeof(text), None)  # 写入内存
    print(text.value.decode('GBK'))  # 取出内存中的TEXT解码
    # print(win32gui.SendMessage(hWnd, commctrl.TB_DELETEBUTTON, i, textPointer))  # 删除图标