Python win32gui SetAsForegroundWindow函数无法正常工作

Aas*_*daq 12 python windows win32gui setforegroundwindow

我正在尝试通过搜索其标题来编写一个找到窗口的程序.一旦找到窗口,它将尝试将其带到前面.我正在使用win32guiAPI来实现这一目标.我能够让它在大多数情况下工作,但由于某种原因,如果任务管理器在前面,它就不起作用.我有以下示例代码.

import win32gui, win32con
import re, traceback
from time import sleep

class cWindow:
    def __init__(self):
        self._hwnd = None

    def BringToTop(self):
        win32gui.BringWindowToTop(self._hwnd)

    def SetAsForegroundWindow(self):
        win32gui.SetForegroundWindow(self._hwnd)

    def Maximize(self):
        win32gui.ShowWindow(self._hwnd, win32con.SW_MAXIMIZE)

    def setActWin(self):
        win32gui.SetActiveWindow(self._hwnd)

    def _window_enum_callback(self, hwnd, wildcard):
        '''Pass to win32gui.EnumWindows() to check all the opened windows'''
        if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) != None:
            self._hwnd = hwnd

    def find_window_wildcard(self, wildcard):
        self._hwnd = None
        win32gui.EnumWindows(self._window_enum_callback, wildcard)


def main():
    sleep(5)
    try:      
        wildcard = ".*Building Operation WorkStation.*"
        cW = cWindow()
        cW.find_window_wildcard(wildcard)
        cW.Maximize()
        cW.BringToTop()
        cW.SetAsForegroundWindow()

    except:
        f = open("log.txt", "w")
        f.write(traceback.format_exc())
        print traceback.format_exc()
main()
Run Code Online (Sandbox Code Playgroud)

我从多个在线资源中拼凑出来.它似乎在很大程度上起作用,但对于像任务管理器这样的某些窗口,它有时会工作但是其余部分都会失败.当它无法正常工作时,我注意到应用程序图标闪烁黄色.有没有一种正确的方法来确保我感兴趣的窗口被设置为100%的前景?我不确定这是否相关,但我使用的是Windows 7 Professional(32位)和Service Pack 1.

mkl*_*nt0 6

注意:以下内容仅涉及确保在激活窗口之前隐藏任务管理器等永远在顶的窗口 - 它假定激活部件本身工作正常,可能不是这种情况.此处SetForegroundWindow列出允许进程调用Windows API函数的条件.


任务管理器在两个方面是特殊的:

  • 默认情况下,它设置为始终显示在顶部,即高于所有其他窗口.
  • 即使关闭(Options > Always on Top 未选中),您仍然可以将其显示在其他始终在顶部的窗口上(普通窗口看似无法做到的事情).

你的代码:

  • 正在工作 - 在我的测试中 - 在目标窗口确实成为活动窗口的意义上.
  • 不是在任务管理器窗口仍停留在(最大化)窗口的顶部感工作.
    • 不幸的是,即使试图让你的窗户永远在窗户也无济于事.

具体检查是否存在任务管理器窗口并将其最小化是一个选项,但请注意,可能还有其他始终在顶部的窗口,因此对于强大的解决方案,您必须识别所有打开的始终在顶的窗口并最小化他们:

以下尝试很难识别除任务栏和开始按钮之外的所有永远在顶的窗口,并最小化(有效地隐藏)任何此类窗口.

新的方法是hide_always_on_top_windows_window_enum_callback_hide.

import win32gui, win32con
import re, traceback
from time import sleep

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)

def main():
    sleep(5)
    try:      
        regex = ".*Building Operation WorkStation.*"
        cW = cWindow()
        cW.find_window_regex(regex)
        cW.Maximize()
        cW.SetAsForegroundWindow()

    except:
        f = open("log.txt", "w")
        f.write(traceback.format_exc())
        print(traceback.format_exc())
main()
Run Code Online (Sandbox Code Playgroud)


Tig*_*222 5

我找到了一个解决方案:如果是taskmanager,那就杀了它.我添加了一个方法cWindow:

def kill_task_manager(self):
    # Here I use your method to find a window because of an accent in my french OS,
    # but you should use win32gui.FindWindow(None, 'Task Manager complete name').
    wildcard = 'Gestionnaire des t.+ches de Windows'
    self.find_window_wildcard(wildcard)
    if self._hwnd:
        win32gui.PostMessage(self._hwnd, win32con.WM_CLOSE, 0, 0)  # kill it
        sleep(0.5)  # important to let time for the window to be closed
Run Code Online (Sandbox Code Playgroud)

之后调用此方法cW = cWindow().

另一个错误陷阱是防止此异常SetAsForegroundWindow:

error: (0, 'SetForegroundWindow', 'No error message is available')
Run Code Online (Sandbox Code Playgroud)

只需在win32gui调用之前发送一个alt键:

# Add this import
import win32com.client

# Add this to __ini__
self.shell = win32com.client.Dispatch("WScript.Shell")

# And SetAsForegroundWindow becomes
def SetAsForegroundWindow(self):
    self.shell.SendKeys('%')
    win32gui.SetForegroundWindow(self._hwnd)
Run Code Online (Sandbox Code Playgroud)

最后,如果我可以,不要比较!= None但是is not None.更pythonic;)

这是完整的代码:

# coding: utf-8

import re, traceback
import win32gui, win32con, win32com.client
from time import sleep


class cWindow:
    def __init__(self):
        self._hwnd = None
        self.shell = win32com.client.Dispatch("WScript.Shell")

    def BringToTop(self):
        win32gui.BringWindowToTop(self._hwnd)

    def SetAsForegroundWindow(self):
        self.shell.SendKeys('%')
        win32gui.SetForegroundWindow(self._hwnd)

    def Maximize(self):
        win32gui.ShowWindow(self._hwnd, win32con.SW_MAXIMIZE)

    def setActWin(self):
        win32gui.SetActiveWindow(self._hwnd)

    def _window_enum_callback(self, hwnd, wildcard):
        '''Pass to win32gui.EnumWindows() to check all the opened windows'''
        if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None:
            self._hwnd = hwnd

    def find_window_wildcard(self, wildcard):
        self._hwnd = None
        win32gui.EnumWindows(self._window_enum_callback, wildcard)

    def kill_task_manager(self):
        wildcard = 'Gestionnaire des t.+ches de Windows'
        self.find_window_wildcard(wildcard)
        if self._hwnd:
            win32gui.PostMessage(self._hwnd, win32con.WM_CLOSE, 0, 0)
            sleep(0.5)

def main():
    sleep(5)
    try:
        wildcard = ".*Building Operation WorkStation.*"
        cW = cWindow()
        cW.kill_task_manager()
        cW.find_window_wildcard(wildcard)
        cW.BringToTop()
        cW.Maximize()
        cW.SetAsForegroundWindow()

    except:
        f = open("log.txt", "w")
        f.write(traceback.format_exc())
        print(traceback.format_exc())


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

来源:如何使用Python中的win32guiwin32gui.SetActiveWindow()来关闭带句柄的窗口错误:找不到指定的过程.