import win32gui import win32con import win32api

获取指定程序的所有窗口句柄

def get_window_handles(program_name): handles = [] def callback(hwnd, hwnds): if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd).startswith(program_name): hwnds.append(hwnd) return True win32gui.EnumWindows(callback, handles) return handles

隐藏指定程序的所有窗口

def hide_windows(program_name): handles = get_window_handles(program_name) for hwnd in handles: win32gui.ShowWindow(hwnd, win32con.SW_HIDE)

显示指定程序的所有窗口

def show_windows(program_name): handles = get_window_handles(program_name) for hwnd in handles: win32gui.ShowWindow(hwnd, win32con.SW_SHOW)

要操作窗口的程序名称

program_name = 'notepad.exe'

隐藏窗口

hide_windows(program_name)

要执行的程序路径

executable_path = r'C:\Windows\System32\notepad.exe'

执行程序

win32api.ShellExecute(0, 'open', executable_path, '', '', win32con.SW_HIDE)

显示窗口

show_windows(program_name)

Python 隐藏和显示指定程序的所有窗口 - 代码优化

原文地址: https://www.cveoy.top/t/topic/nJpS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录