用python实现老板键功能

主要实现目标:为多个指定的程序实现统一的老板键,一键隐藏多个指定的应用程序的窗口及任务栏。

1.获取所有顶层窗口

import win32gui

hwnd_title = dict)

def get_all_hwndhwnd, mouse):
    # 判断句柄是否为窗口、窗口是否允许输入、窗口是否可视
    if win32gui.IsWindowhwnd) and win32gui.IsWindowEnabledhwnd) and win32gui.IsWindowVisiblehwnd):
        hwnd_title.update{hwnd: win32gui.GetWindowTexthwnd)})

# EnumWindows枚举所有顶层窗口
win32gui.EnumWindowsget_all_hwnd, 0)

# 打印出所有窗口名不为空的窗口
for key in listhwnd_title):
    if hwnd_title[key] is "":
        del hwnd_title[key]

2.手动选择需要设置老板键的程序


import tkinter as tk

root = tk.Tk)
root.geometry"800x400")

# 列表显示所有顶层窗口
listBox = tk.Listboxroot, selectmode="multiple")
listBox.packside="left", expand="yes", fill="both")

for i, j in hwnd_title.items):
    if j is not "":
        listBox.insert"end", stri) + ":" + j)

bt = tk.Buttonroot, text='选择')
bt.pack)

root.mainloop)


3.隐藏或显示选中程序


# 通过GetWindowRect方法获取隐藏前的窗口位置及尺寸信息
left, top, right, bottom = win32gui.GetWindowRect)

def close_windowsaimLists):
    for k in aimLists:
        # 隐藏程序窗口
        win32gui.SetWindowPoslists[k][0], 0, 0, 0, 0, 0, SWP_HIDEWINDOW)


def open_windowsaimLists):
    for k in aimLists:
        # 显示程序窗口
        t = lists[k]
        win32gui.SetWindowPost['hwnd'], 0, t['left'], t['top'], t['right'] - t['left'], t['bottom'] - t['top'],
                              SWP_SHOWWINDOW)

4.设置显示隐藏快捷键

这里设置F7显示,F8隐藏,F12中止


import PyHook3
import pythoncom

def onKeyboardEventevent):
    key = event.Key
    if key == "F7":
        close_windowsaimLists)
    if key == "F8":
        open_windowsaimLists)
    if key == "F12":
        win32gui.PostQuitMessage0)
    return True

hm = PyHook3.HookManager)
hm.KeyDown = onKeyboardEvent
hm.HookKeyboard)

# 开启监听
pythoncom.PumpMessages)

5.最终效果

Published by

风君子

独自遨游何稽首 揭天掀地慰生平