需要使窗口处于活动状态

时间:2021-03-20 13:36:59

标签: python windows winapi autoit

我尝试使用两个库:

  1. win32
  2. 自动

但它们都不能正常工作。

代码示例:

import autoit
import win32gui


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

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

1 个答案:

答案 0 :(得分:0)

通过类名查找窗口的函数:

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)