-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow_manager.py
More file actions
49 lines (40 loc) · 1.78 KB
/
window_manager.py
File metadata and controls
49 lines (40 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import win32gui
import win32con
import win32process
import win32api
import logging
def force_activate_hwnd(hwnd, show_cmd):
try:
if not win32gui.IsWindow(hwnd):
logging.warning(f"Target HWND {hwnd} is no longer valid.")
return False
current_thread = win32api.GetCurrentThreadId()
target_thread, _ = win32process.GetWindowThreadProcessId(hwnd)
foreground_thread, _ = win32process.GetWindowThreadProcessId(win32gui.GetForegroundWindow())
if current_thread != foreground_thread:
win32process.AttachThreadInput(current_thread, foreground_thread, True)
if target_thread != foreground_thread:
win32process.AttachThreadInput(target_thread, foreground_thread, True)
win32gui.ShowWindow(hwnd, show_cmd)
win32gui.SetForegroundWindow(hwnd)
if current_thread != foreground_thread:
win32process.AttachThreadInput(current_thread, foreground_thread, False)
if target_thread != foreground_thread:
win32process.AttachThreadInput(target_thread, foreground_thread, False)
logging.info(f"Successfully activated HWND: {hwnd}")
return True
except Exception as e:
logging.error(f"Failed to activate HWND {hwnd}: {e}", exc_info=True)
return False
def get_all_windows():
windows = []
def callback(hwnd, extra):
if win32gui.IsWindowVisible(hwnd):
title = win32gui.GetWindowText(hwnd).strip()
if title and title not in ["Program Manager", "ContextSwitch"]:
windows.append((hwnd, title))
try:
win32gui.EnumWindows(callback, None)
except Exception as e:
logging.error(f"Failed to enumerate windows: {e}", exc_info=True)
return windows