You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用win32gui检测RAGE Multiplayer活动窗口失败的技术问询

问题:win32gui检测RAGE Multiplayer窗口始终返回False

使用win32gui获取活动窗口名称并验证激活状态时,VSCode窗口检测正常,但针对"RAGE Multiplayer"窗口的判断始终返回False。

当前代码:

while True:
    if keyboard.is_pressed('F3'):
        window = win32gui.GetWindowText(win32gui.GetForegroundWindow());
        print(window)
        if window == 'RAGE Multiplayer':
            startloop = True
            print('Game is open')
            break
        elif window == 'test.py - Visual Studio Code':
            print('window is open')
        else:
            print('close')

怀疑游戏窗口需要特殊检测方法,请求协助排查。


排查方案

  • 验证窗口标题一致性:游戏窗口标题可能包含不可见字符、全角空格或额外后缀,将print(window)替换为print(repr(window)),输出原始字符串格式,对比是否与"RAGE Multiplayer"完全匹配。
  • 通过进程ID关联窗口:先从任务管理器获取RAGE Multiplayer的进程ID,再遍历窗口找到对应进程的窗口,判断是否为前台窗口:
    import win32gui
    import win32process
    
    def get_window_by_pid(target_pid):
        def callback(hwnd, extra):
            _, pid = win32process.GetWindowThreadProcessId(hwnd)
            if pid == target_pid and win32gui.IsWindowVisible(hwnd):
                extra.append(hwnd)
            return True
        hwnds = []
        win32gui.EnumWindows(callback, hwnds)
        return hwnds
    
    # 替换为RAGE Multiplayer的实际进程ID
    rage_pid = 12345
    rage_windows = get_window_by_pid(rage_pid)
    foreground_hwnd = win32gui.GetForegroundWindow()
    if foreground_hwnd in rage_windows:
        print('Game is open')
    
  • 检查子窗口:部分游戏会用子窗口作为显示层,尝试获取前台窗口的子窗口并读取标题:
    import win32gui
    import win32con
    
    foreground_hwnd = win32gui.GetForegroundWindow()
    child_hwnd = win32gui.GetWindow(foreground_hwnd, win32con.GW_CHILD)
    child_title = win32gui.GetWindowText(child_hwnd)
    print(child_title)
    
  • 提升脚本权限:以管理员身份运行Python脚本,部分游戏窗口需要更高权限才能读取窗口信息。

内容的提问来源于stack exchange,提问作者Marv Lebett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 09:07:47