Windows 7: how to bring a window to the front no matter what other window has focus?

后端 未结 9 1790
走了就别回头了
走了就别回头了 2020-11-28 08:04

I\'m implementing a task-bar replacement, dock-like application-switcher style program. It\'s doing some unique stuff with OpenGL, and with keyboard shortcuts, so the way it

9条回答
  •  心在旅途
    2020-11-28 08:37

    I saw some great answers above, but needed extra functionality where window name would be a more flexible parameter. On failure it returns false:

    from win32gui import IsWindowVisible, GetWindowText, EnumWindows,\
    ShowWindow, SetForegroundWindow, SystemParametersInfo
    
    #Sub-Functions
    def window_enum_handler(hwnd, resultList):
        if IsWindowVisible(hwnd) and GetWindowText(hwnd) != '':
            resultList.append((hwnd, GetWindowText(hwnd)))
    
    #Prime-Functions
    def winFocus(partial_window_name):
        SystemParametersInfo(8193, 0, 2 | 1)
        handles=[]
        EnumWindows(window_enum_handler, handles)
        for i in handles:
            if str(partial_window_name).upper() in str(i[1]).upper():
                ShowWindow(i[0], 3)
                SetForegroundWindow(i[0])
                return True
        print(partial_window_name + " was not found")
        return False
    
    winFocus("not existing window")
    winFocus("ChroME")
    

提交回复
热议问题