G20要写一个轮询几个重要页面的程序,不停的在大屏上进行刷新,通过pywin32模块下的SetForegroundWindow函数调用时,会出现error: (0, ‘SetForegroundWindow’, ‘No error message is available’)报错,后经网上查询确认,为pywin32模块下的一个小bug,在该函数调用前,需要先发送一个其他键给屏幕,如ALT键 。

对SetForegroundWindow进行重新封装以后的结果如下:

1# Add this import
2import win32com.client
3# Add this to __ini__
4self.shell = win32com.client.Dispatch("WScript.Shell")
5# And SetAsForegroundWindow becomes
6def SetAsForegroundWindow(self):
7    #发送ALT键,ALT键使用%号表示
8    self.shell.SendKeys('%')
9    win32gui.SetForegroundWindow(self._hwnd)

为完善常用的调用场景,如窗口最大化、窗口隐藏、窗口放在最前面等,这里可以通过一个类方便的实现,如下:

 1# coding: utf-8
 2import re, traceback
 3import win32gui, win32con, win32com.client
 4from time import sleep
 5class cWindow:
 6    def __init__(self):
 7        self._hwnd = None
 8        self.shell = win32com.client.Dispatch("WScript.Shell")
 9    def BringToTop(self):
10        win32gui.BringWindowToTop(self._hwnd)
11    def SetAsForegroundWindow(self):
12        self.shell.SendKeys('%')
13        win32gui.SetForegroundWindow(self._hwnd)
14    def Maximize(self):
15        win32gui.ShowWindow(self._hwnd, win32con.SW_MAXIMIZE)
16    def setActWin(self):
17        win32gui.SetActiveWindow(self._hwnd)
18    def _window_enum_callback(self, hwnd, wildcard):
19        '''Pass to win32gui.EnumWindows() to check all the opened windows'''
20        if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None:
21            self._hwnd = hwnd
22    def find_window_wildcard(self, wildcard):
23        self._hwnd = None
24        win32gui.EnumWindows(self._window_enum_callback, wildcard)
25    def kill_task_manager(self):
26        wildcard = 'Gestionnaire des t.+ches de Windows'
27        self.find_window_wildcard(wildcard)
28        if self._hwnd:
29            win32gui.PostMessage(self._hwnd, win32con.WM_CLOSE, 0, 0)
30            sleep(0.5)
31def main():
32    sleep(5)
33    try:
34        wildcard = ".*Building Operation WorkStation.*"
35        cW = cWindow()
36        cW.kill_task_manager()
37        cW.find_window_wildcard(wildcard)
38        cW.BringToTop()
39        cW.Maximize()
40        cW.SetAsForegroundWindow()
41    except:
42        f = open("log.txt", "w")
43        f.write(traceback.format_exc())
44        print(traceback.format_exc())
45if __name__ == '__main__':
46    main()

上面的操作已经很不错了,有人对此提出了更近一步的优化,表示在某此情况下,该脚本不能正常工作,又封装了两个函数,重新封装的类如下:

 1import win32gui, win32con
 2import re, traceback
 3from time import sleep
 4class cWindow:
 5    def __init__(self):
 6        self._hwnd = None
 7    def SetAsForegroundWindow(self):
 8        # First, make sure all (other) always-on-top windows are hidden.
 9        self.hide_always_on_top_windows()
10        win32gui.SetForegroundWindow(self._hwnd)
11    def Maximize(self):
12        win32gui.ShowWindow(self._hwnd, win32con.SW_MAXIMIZE)
13    def _window_enum_callback(self, hwnd, regex):
14        '''Pass to win32gui.EnumWindows() to check all open windows'''
15        if self._hwnd is None and re.match(regex, str(win32gui.GetWindowText(hwnd))) is not None:
16            self._hwnd = hwnd
17    def find_window_regex(self, regex):
18        self._hwnd = None
19        win32gui.EnumWindows(self._window_enum_callback, regex)
20    def hide_always_on_top_windows(self):
21        win32gui.EnumWindows(self._window_enum_callback_hide, None)
22    def _window_enum_callback_hide(self, hwnd, unused):
23        if hwnd != self._hwnd: # ignore self
24            # Is the window visible and marked as an always-on-top (topmost) window?
25            if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) & win32con.WS_EX_TOPMOST:
26                # Ignore windows of class 'Button' (the Start button overlay) and
27                # 'Shell_TrayWnd' (the Task Bar).
28                className = win32gui.GetClassName(hwnd)
29                if not (className == 'Button' or className == 'Shell_TrayWnd'):
30                    # Force-minimize the window.
31                    # Fortunately, this seems to work even with windows that
32                    # have no Minimize button.
33                    # Note that if we tried to hide the window with SW_HIDE,
34                    # it would disappear from the Task Bar as well.
35                    win32gui.ShowWindow(hwnd, win32con.SW_FORCEMINIMIZE)
36def main():
37    sleep(5)
38    try:
39        regex = ".*Building Operation WorkStation.*"
40        cW = cWindow()
41        cW.find_window_regex(regex)
42        cW.Maximize()
43        cW.SetAsForegroundWindow()
44    except:
45        f = open("log.txt", "w")
46        f.write(traceback.format_exc())
47        print(traceback.format_exc())
48main()

code原页面如下:http://qaoverflow.com/question/python-win32gui-setasforegroundwindow-function-not-working-properly/