import win32gui
import re
class WindowMgr:
"""Encapsulates some calls to the winapi for window management"""
def __init__ (self):
"""Constructor"""
self._handle = None
def find_window(self, class_name, window_name=None):
"""find a window by its class_name"""
self._handle = win32gui.FindWindow(class_name, window_name)
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._handle = hwnd
def find_window_wildcard(self, wildcard):
"""find a window whose title matches the wildcard regex"""
self._handle = None
win32gui.EnumWindows(self._window_enum_callback, wildcard)
def set_foreground(self):
"""put the window in the foreground"""
win32gui.SetForegroundWindow(self._handle)
w = WindowMgr()
w.find_window_wildcard(".*Hello.*")
w.set_foreground()
import pywinauto
# SWAPY will record the title and class of the window you want activated
app = pywinauto.application.Application()
t, c = u'WINDOW SWAPY RECORDS', u'CLASS SWAPY RECORDS'
handle = pywinauto.findwindows.find_windows(title=t, class_name=c)[0]
# SWAPY will also get the window
window = app.window_(handle=handle)
# this here is the only line of code you actually write (SWAPY recorded the rest)
window.SetFocus()
根据您如何识别所需的窗口,有一些方法可以获得hwnd(又名窗口句柄)。 你可以通过user32.GetWindowThreadProcessId根据pid或者通过user32.GetWindowTextW根据窗口名loop through all the windows来找到正确的句柄 要获取ProcessIds,您可以使用Windows内置的wmic。您可以使用它执行许多其他漂亮的操作。(所有属性:wmic process get /?)(get handle是断开的tho)例如:
def get_pids(proc_name):
out = subprocess.check_output('wmic process where Name="%s" get ProcessId' % proc_name)
pids = out.decode().strip().split()[1:]
if not pids:
raise WindowsError('Could not find pids for process')
return [int(pid) for pid in pids]
import win32gui
import re
class WindowMgr:
"""Encapsulates some calls to the winapi for window management"""
def __init__ (self):
"""Constructor"""
self._handle = None
self._handles = []
def find_window(self, class_name, window_name=None):
"""find a window by its class_name"""
self._handle = win32gui.FindWindow(class_name, window_name)
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._handles.append(hwnd)
self._handle = hwnd
def find_window_wildcard(self, wildcard):
"""find a window whose title matches the wildcard regex"""
self._handle = None
self._handles = []
win32gui.EnumWindows(self._window_enum_callback, wildcard)
self.set_handle()
def set_foreground(self):
"""put the window in the foreground"""
if self._handle != None:
win32gui.SetForegroundWindow(self._handle)
else:
print("No handle is selected, couldn't set focus")
def set_handle(self):
"""get one handle to operate on from all the matched handles"""
if len(self._handles) < 1:
print("Matched no window")
return False
if len(self._handles) > 1:
print("Selecting the first handle of multiple windows:")
else: # len(self._handles) == 1:
print("Matched a single window:")
self.print_matches()
self._handle = self._handles[0]
return True
def print_matches(self):
"""print the title of each matched handle"""
for hwnd in self._handles:
print("- " + str(win32gui.GetWindowText(hwnd)))
w = WindowMgr()
w.find_window_wildcard(".*Hello.*")
w.set_foreground()
7条答案
按热度按时间gzjq41n41#
你可以使用win32gui模块来完成这个任务。首先你需要在你的窗口上获得一个有效的句柄。如果你知道窗口的类名或者确切的标题,你可以使用
win32gui.FindWindow
。如果不知道,你可以用win32gui.EnumWindows
枚举窗口,然后尝试找到正确的窗口。一旦你有了句柄,你就可以用这个句柄调用
win32gui.SetForegroundWindow
,它将激活窗口,并准备好接受你的击键。请看下面的一个例子。我希望它能有所帮助
zbwhf8kr2#
Pywinauto和SWAPY可能需要最少的工作to set the focus of a window。
使用SWAPY自动生成检索窗口对象所需的python代码,例如:
如果其他窗口碰巧在感兴趣的窗口前面,这不是一个问题。This additional code或这将确保它在运行上面的代码之前显示:
brvekthn3#
我们开始吧。你只需要存储活动窗口的值。
5sxhfpxr4#
Pip安装键盘。在你设置前台窗口之前,模拟一个键盘到esc,这是键盘。send('esc ')你可能想做三次以下任一:
1.侧边栏
1.始终位于顶层的任务管理器
mv1qrgav5#
使用
SetWindowPos
或SetForegroundWindow
可能不够,如果窗口是minifiedakaIsIconic
!我们可以使用ShowWindow
与SW_RESTORE
(9):根据您如何识别所需的窗口,有一些方法可以获得
hwnd
(又名窗口句柄)。你可以通过
user32.GetWindowThreadProcessId
根据pid
或者通过user32.GetWindowTextW
根据窗口名loop through all the windows来找到正确的句柄要获取ProcessIds,您可以使用Windows内置的
wmic
。您可以使用它执行许多其他漂亮的操作。(所有属性:wmic process get /?
)(get handle
是断开的tho)例如:s71maibg6#
保持窗口活动的GUI应用程序
Python3
alive.pyw
**文件 *yjghlzjz7#
为了补充@luc的答案,下面是当存在多个窗口时,代码如何更详细地说明它所选择的句柄:
pip install pywin32
之后,运行