우리는 openframeworks로 제작 된 앱을 가지고 있습니다. 시작되면 먼저 콘솔 창을 열고 작업을 수행 한 다음 열려있는 상태로 유지 한 다음 각 모니터에 하나씩 전체 화면으로 창을 여는 두 개의 자식 프로세스를 시작합니다. 앱을 제작하는 사람에 따르면 두 개의 창 제목을 제공하는 것은 불가능합니다.제목이없는 창에 포커스를 가져 오는 방법은 무엇입니까?
내 작업은 스크립트를 구축하는 것입니다 : 응용 프로그램이 충돌 한 경우
- 확인을하고 창문이 전경에 있고 그들 중 하나에 초점이 있는지 확인합니다 그것을 다시 열립니다하고있는 경우이를 수정 그들은 그렇지 않다.
정확하게 이것을 한 오래된 파이썬 스크립트를 재사용하고 법안에 맞게 수정하고 싶다.
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 영화 프로그램과 같은 무작위 프로그램을 여는 이름이없는 창을 확인하는 것입니다 (또는 적어도 이상한 포 그라운드로 가져옵니다). 왜냐하면 그들은 달려서는 안되기 때문입니다.)
창에 이름을 알지 못하고 초점을 맞출 수있는 더 좋은 방법이 있습니까? 내 생각 중 하나는 부모 프로세스를 얻은 다음 거기에서 자식에게 액세스하여 어떻게 든 포커스를 가져 오는 것이었지만 방법을 파악할 수 없었습니다.
파이썬을 사용하는 것보다 더 좋은 방법을 얻을 수 있다면, 그 방향으로 어떤 포인터라도 기쁘게 생각할 것입니다. 당신은 고유의 창 클래스 이름이있는 경우
이 코드는 응용 프로그램 자체에 넣을 코드입니다. 나는 내 대본에서 이것으로 무엇을 할 것인지 모르겠다. –
아니요, getCalssName이 도움이되지 않으면 프로세스 이미지의 이름을 얻는 데 사용할 수있는 API 샘플/EXE 파일입니다. –
어쩌면이 [64 비트 Windows에서 실행중인 프로세스 나열] (https://stackoverflow.com/a/1632274/8918119)이 파이썬 방식 일 것입니다. –