安装依赖
pip install pywin32
实现代码
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# ======================================================
# @File: : video_auto
# @Author : forward_huan
# @Date : 2024/2/25 14:39
# @Desc :
# ======================================================
import win32gui
import win32con
import win32com.client
# region 读取已打开应用并且显示到前面
def get_all_hwnd():
def impl(hwnd, *args):
if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
hwnd_map.update({hwnd: win32gui.GetWindowText(hwnd)})
hwnd_map = {}
win32gui.EnumWindows(impl, 0)
return hwnd_map
def window_show_top(window_title):
hwnd_map = get_all_hwnd()
for handle, title in hwnd_map.items():
if not title or title != window_title:
continue
win32gui.BringWindowToTop(handle)
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
# 被其他窗口遮挡,调用后放到最前面
win32gui.SetForegroundWindow(handle)
# 解决被最小化的情况
win32gui.ShowWindow(handle, win32con.SW_RESTORE)
# endregion
window_show_top("夜神模拟器")