我正在尝试通过搜索其标题来编写一个找到窗口的程序.一旦找到窗口,它将尝试将其带到前面.我正在使用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 …Run Code Online (Sandbox Code Playgroud) 首先,我知道这是一个有争议的讨论,但我希望我们能保持这种技术.
我有一个在后台启动的应用程序,我不知何故希望它能够激活/带来在不同的进程中聚焦窗口.但是,即使我要激活其窗口的进程调用了AllowSetForegroundWindow(ASFW_ANY),调用SetForegroundWindow总是会失败.
原因是(IMO)启动应用程序是后台进程,并且由于它没有收到输入,因此不允许设置前台窗口.所以一切都出现在任务列表中,但没有显示.
所以我尝试创建一个虚拟窗口来接收输入,该输入随意关闭,然后能够成功调用SetForegroundWindow.但即使是我显示的虚拟窗口也会在后台显示.
但是,如果我打电话
AttachThreadInput(
GetWindowThreadProcessId(GetForegroundWindow(), NULL),
GetCurrentThreadId(), TRUE);
Run Code Online (Sandbox Code Playgroud)
创建虚拟窗口前,在前景确实是创建的窗口,我可以以后在不同的进程,其作品叫SetForegroundWindow一个不同的HWND.
但是:如果我不创建虚拟窗口,尽管我使用AttachThreadInput,SetForegroundWindow仍然返回零.
我不明白为什么如果我创建一个自己的Window(并且之后成功为其他窗口),AttachThreadInput hack成功,但如果我不首先创建自己的窗口则不成功.
我的后台进程如何在不创建虚拟窗口的情况下在不同进程中的另一个窗口上调用SetForegroundWindow ?
[*]后台应用程序实际上是gpg-agent.exe,只要请求密码,就会调用pinentry.exe(我的应用程序).pinentry.exe(作为后台进程运行)必须从另一个正在运行的应用程序请求密码,因此它必须将其窗口置于前台...
我用来pygetwindow通过它的标题将一个窗口带到前台。如果我从 cmd(和 PowerShell)运行 python 脚本,它会完美地工作。但是,如果我从任何其他终端(例如 Alacritty)运行它,提到的窗口不会进入前台,而是开始在任务栏中闪烁。
为什么会这样呢?我已将 Alacritty 配置为使用 cmd。
以下是我的代码的相关部分:
import pygetwindow
try:
window = pygetwindow.getWindowsWithTitle('foobar')[0]
window.activate()
except Exception as e:
#raise e
print("open foobar please")
exit(1)
Run Code Online (Sandbox Code Playgroud)
以下是我的 alacritty 配置的相关部分
# Shell
# You can set `shell.program` to the path of your favorite shell, e.g. `/bin/fish`.
# Entries in `shell.args` are passed unmodified as arguments to the shell.
# Default:
# - (macOS) /bin/bash --login
# - (Linux/BSD) user login shell
# - (Windows) powershell
shell:
program: …Run Code Online (Sandbox Code Playgroud) 我正在从事一个自动化项目,该项目要求我搜索 acrobat 进程,然后将其置于前台。我使用的是 Windows Server 2012 R2 Standard 和 Python。我在进程列表中搜索该特定 PDF 文件,然后尝试将该窗口置于前台。
当服务器打开并处于活动状态时,我的代码运行良好。一旦它被最小化或锁定,它就不起作用。具体来说,SetForegroundWindow() 行不起作用并抛出错误“(0, 'SetForegroundWindow', 'No error message is available')”。
接下来,我浏览了此处列出的许多示例,有人建议在该行之前使用“Alt”选项卡。试过了,没用。然后我根据另一个用户使用 ShowWindow() 但又出现了同样的错误。现在,我完全被困住了。
我已经查看了许多与该问题相关的线程,例如: 1. SetForegroundWindow 不适用于最小化进程 2.最小化时前移窗口 3.最小化时前移窗口
我试过 MainWindowHandle 可以修复错误,但我无法在 python 中实现它。
另外,我安装了 pywinauto 并使用了 pywinauto 的 findwindows 和 SetForegroundWindow 功能。当服务器处于活动状态时它运行良好,但是一旦我们最小化或锁定服务器,它就不起作用并给出相同的错误“(0, 'SetForegroundWindow', 'No error message is available')”。
现在,我完全没有选择。任何想法/建议?
这是片段:
import win32gui,win32con
import win32com.client
import time
time.sleep(3)
def windowEnumerationHandler(hwnd, top_windows):
if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd)!='':
top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
results = []
top_windows = []
file_name_of_pdf = "ARW_AR_2016.pdf"
win32gui.EnumWindows(windowEnumerationHandler, …Run Code Online (Sandbox Code Playgroud) python ×2
win32gui ×2
alacritty ×1
cmd ×1
python-3.x ×1
pywinauto ×1
winapi ×1
window ×1
windows ×1
windows-10 ×1