需要激活 window

Need to make the window active

我尝试使用两个库:

  1. win32
  2. 自动

但是其中 none 个可以正常工作。

代码示例:

import autoit
import win32gui


hWnd = autoit.win_get_handle('[CLASS:Notepad]')

win32gui.SetActiveWindow(hWnd)
autoit.win_active_by_handle(hWnd)

通过 class 名称查找 window 的函数:

import win32gui

def window_get(window=None, class_name:str=None)->int:
    ''' Returns hwnd. If window is not specified then
        finds foreground window.
    '''
    if isinstance(window, str):
        return win32gui.FindWindow(class_name, window)
    elif isinstance(window, int):
        return window
    elif not window and class_name:
        return win32gui.FindWindow(class_name, window)
    else:
        return win32gui.GetForegroundWindow()

用法:

import win32con

hwnd = window_get(class_name='Notepad')
win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
win32gui.SetForegroundWindow(hwnd)