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)

posted @ 2020-12-02 13:52  myrj  阅读(1216)  评论(1编辑  收藏  举报