• 激活浏览器窗口


    import win32.win32gui as win32gui
    import win32.lib.win32con as win32con
        class cWindow:
            def __init__(self):
                self._hwnd = None
    
            def SetAsForegroundWindow(self):
                # First, make sure all (other) always-on-top windows are hidden.
                # self.hide_always_on_top_windows()
                win32gui.SetForegroundWindow(self._hwnd)
    
            def Maximize(self):
                win32gui.ShowWindow(self._hwnd, win32con.SW_MAXIMIZE)
    
            def _window_enum_callback(self, hwnd, regex):
                '''Pass to win32gui.EnumWindows() to check all open windows'''
                if self._hwnd is None and re.match(regex, str(win32gui.GetWindowText(hwnd))) is not None:
                    self._hwnd = hwnd
    
            def find_window_regex(self, regex):
                self._hwnd = None
                win32gui.EnumWindows(self._window_enum_callback, regex)
    
            def hide_always_on_top_windows(self):
                win32gui.EnumWindows(self._window_enum_callback_hide, None)
    
            def _window_enum_callback_hide(self, hwnd, unused):
                if hwnd != self._hwnd:  # ignore self
                    # Is the window visible and marked as an always-on-top (topmost) window?
                    if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowLong(hwnd,
                                                                                 win32con.GWL_EXSTYLE) & win32con.WS_EX_TOPMOST:
                        # Ignore windows of class 'Button' (the Start button overlay) and
                        # 'Shell_TrayWnd' (the Task Bar).
                        className = win32gui.GetClassName(hwnd)
                        if not (className == 'Button' or className == 'Shell_TrayWnd'):
                            # Force-minimize the window.
                            # Fortunately, this seems to work even with windows that
                            # have no Minimize button.
                            # Note that if we tried to hide the window with SW_HIDE,
                            # it would disappear from the Task Bar as well.
                            win32gui.ShowWindow(hwnd, win32con.SW_FORCEMINIMIZE)
    
    
        if __name__ == '__main__':
            # regex = ".*11i.*Internet Explorer.*"
            regex = ".*百度.*"
            cW = cWindow()
            cW.find_window_regex(regex)
            cW.Maximize()
            cW.SetAsForegroundWindow()
  • 相关阅读:
    1040 最大公约数之和(欧拉函数)
    1028 大数乘法 V2(FFT or py)
    1020 逆序排列(DP)
    1837 砝码称重
    1070 Bash游戏 V4
    1280 前缀后缀集合(map)
    1390 游戏得分(贪心)
    1179 最大的最大公约数
    1400 序列分解(dfs)
    1420 数袋鼠好有趣(贪心二分)
  • 原文地址:https://www.cnblogs.com/lnd-blog/p/14244834.html
Copyright © 2020-2023  润新知