Python 选择windows应用窗口到最前面

本文介绍了如何使用Python和Pywin32库来获取Windows系统中所有活动窗口,并将指定的夜神模拟器窗口调整至最前面,实现窗口管理和控制功能。
摘要由CSDN通过智能技术生成

安装依赖

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("夜神模拟器")

  • 12
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值