gpt4 book ai didi

Python win32gui SetAsForegroundWindow 函数无法正常工作

转载 作者:太空狗 更新时间:2023-10-29 17:47:41 26 4
gpt4 key购买 nike

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

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()

我从多个在线资源中拼凑了这个。它似乎在大多数情况下都可以工作,但对于某些窗口(如任务管理器),它有时会工作但其余部分会失败。当它不能正常工作时,我只注意到应用程序图标呈黄色闪烁。是否有正确的方法来确保我感兴趣的窗口在 100% 的时间内设置为前景?我不确定这是否相关,但我使用的是带有 Service Pack 1 的 Windows 7 Professional(32 位)。

最佳答案

我找到了一个解决方案:如果是 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

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

另一个 bug 陷阱是在 SetAsForegroundWindow 中防止这个异常:

error: (0, 'SetForegroundWindow', 'No error message is available')

只需在 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)

最后,如果可以的话,不要比较 != None 不是 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()

来源:how do I close window with handle using win32gui in Pythonwin32gui.SetActiveWindow() ERROR : The specified procedure could not be found .

关于Python win32gui SetAsForegroundWindow 函数无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30200381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com