【发布时间】:2017-11-10 12:13:08
【问题描述】:
我们有一个使用 openframeworks 构建的应用程序。启动时,它首先打开一个控制台窗口,该窗口执行一些工作(并保持打开状态),然后再启动两个子进程,每个子进程都以全屏方式打开一个窗口,每个监视器上都有一个。根据正在构建应用程序的人的说法,不可能给这两个窗口标题。
我的工作是构建一个脚本:
- 检查应用是否崩溃并重新打开
- 验证窗口是否在前景中,其中一个窗口处于焦点位置,如果不是,则修复它们
我想重用我的一个旧 python 脚本,它正是这样做的,并对其进行了修改以适应账单。
from time import sleep
import subprocess
import psutil
import re
import win32gui
import win32con
client_path = "C:\\path_to_app.exe"
window_name = ""
class WindowMgr:
"""Encapsulates some calls to the winapi for window management"""
def __init__(self, ):
"""Constructor"""
self._handle = None
def find_window(self, class_name, window_name=None):
"""find a window by its class_name"""
self._handle = win32gui.FindWindow(class_name, window_name)
def _window_enum_callback(self, hwnd, wildcard):
'''Pass to win32gui.EnumWindows() to check all the opened windows'''
if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None:
self._handle = hwnd
def find_window_wildcard(self, wildcard):
self._handle = None
win32gui.EnumWindows(self._window_enum_callback, wildcard)
def set_foreground(self):
"""put the window in the foreground"""
win32gui.SetForegroundWindow(self._handle)
def maximize(self):
win32gui.ShowWindow(self._handle, win32con.SW_MAXIMIZE)
def is_minimized(self):
return win32gui.IsIconic(self._handle)
def client_crashed():
for pid in psutil.pids():
if psutil.Process(pid).name() == "app.exe":
return False
return True
if __name__ == "__main__":
w = WindowMgr()
w.find_window_wildcard(window_name)
print("Checking")
while True:
if client_crashed() is True:
print("Reopening app.exe")
subprocess.Popen([client_path])
else:
print("Not crashed")
if w.is_minimized:
print("Maximizing")
w.set_foreground()
w.maximize()
else:
print("Not minimized")
print("Sleeping for 10")
sleep(10)
现在检查崩溃和重新启动工作正常。但是由于窗口没有标题,到目前为止我想出的最好的方法是检查没有名称的窗口,这显然会打开像 Windows 10 电影程序这样的随机程序(或者至少将它们带到前台,这很奇怪因为它们不应该运行)。
有没有更好的方法在不知道窗口名称的情况下使窗口成为焦点?我的一个想法是获取父进程,然后从那里访问子进程并以某种方式使它们成为焦点,但我无法弄清楚如何。
如果有比使用 python 更好的方法来实现我想要的,我也会很高兴有任何指向这个方向的指针。
【问题讨论】:
标签: python python-3.x windows-10 openframeworks win32gui