我想獲取特定行程(例如 Spotify.exe)的 Windowtitle。
def winEnumHandler( hwnd, ctx ):
if win32gui.IsWindowVisible( hwnd ):
print (hex(hwnd), win32gui.GetWindowText( hwnd ))
我嘗試了在 Internet 上找到的多個不同版本,但大多數解決方案都針對活動視窗,但在我的情況下,它始終是活動視窗,所以我必須按行程名稱或行程 ID。
所以,基本上我正在尋找這樣的東西
title = getTitleFromProcessName('Spotify.exe')
然后title就是spotify視窗對應的視窗標題。
uj5u.com熱心網友回復:
import win32gui
import win32process
import psutil
import ctypes
EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible
def getProcessIDByName():
qobuz_pids = []
process_name = "Qobuz.exe"
for proc in psutil.process_iter():
if process_name in proc.name():
qobuz_pids.append(proc.pid)
return qobuz_pids
def get_hwnds_for_pid(pid):
def callback(hwnd, hwnds):
#if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
_, found_pid = win32process.GetWindowThreadProcessId(hwnd)
if found_pid == pid:
hwnds.append(hwnd)
return True
hwnds = []
win32gui.EnumWindows(callback, hwnds)
return hwnds
def getWindowTitleByHandle(hwnd):
length = GetWindowTextLength(hwnd)
buff = ctypes.create_unicode_buffer(length 1)
GetWindowText(hwnd, buff, length 1)
return buff.value
def getQobuzHandle():
pids = getProcessIDByName()
for i in pids:
hwnds = get_hwnds_for_pid(i)
for hwnd in hwnds:
if IsWindowVisible(hwnd):
return hwnd
if __name__ == '__main__':
qobuz_handle = getQobuzHandle()
我使用這段代碼解決了它有了這個我得到qobuz視窗的視窗句柄,如果它是打開的,否則它沒有
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411754.html
標籤:
