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