python根据窗口标题找句柄,将窗口前置活动


import time, threading, copy
import win32api, win32con
import win32gui
import win32gui
def zhaojb(aa):
    mu=0
    jh=[]
    hwnd_title = dict()
    def get_all_hwnd(hwnd,mouse):
        if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
            hwnd_title.update({hwnd:win32gui.GetWindowText(hwnd)})
     
    win32gui.EnumWindows(get_all_hwnd, 0)
    for h,t in hwnd_title.items():
        if t is not "":
            if aa in t:
                jh.append(h)
            
    if len(jh)==0:
        print("找不到相应的句柄")
    else:
        return jh
ab=zhaojb("SM")
jbid=ab[0]#有可能找到多个
print(jbid)
win32gui.SetForegroundWindow(jbid)#将相应的窗口前置为活动窗口

 根据进程句柄关闭进程

win32api.TerminateProcess(handle, -1)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM