参考链接:Python中设置指定窗口为前台活动窗口(最顶层窗口)win32gui: https://blog.csdn.net/bailichun19901111/article/details/105042145
Win32 API之EnumWindows详解: http://blog.sina.com.cn/s/blog_e5f00a490101w1yc.html
测试1:SetForegroundWindow修改活动。
#
# 输出当前活动窗体句柄
#
def print_GetForegroundWindow():
hwnd_active = win32gui.GetForegroundWindow()
print('hwnd_active hwnd:',hwnd_active)
print('hwnd_active text:',win32gui.GetWindowText(hwnd_active))
print('hwnd_active class:',win32gui.GetClassName(hwnd_active))
# 输出当前活动窗口句柄
print_GetForegroundWindow()
print('------------------------------------------')
# 设置TIM为活动窗口
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(win32gui.FindWindow("TXGuiFoundation","TIM"))
# 查看是否修改
print_GetForegroundWindow()
输出结果已经修改,但是设置得窗体并不会突出到最前端。这样对于pyautogui来说,是无法操作,因此还需要改进放到最前。
最终结果,可以将置顶窗体置顶最前,且激活。
# 输出当前活动窗口句柄
print_GetForegroundWindow()
print('------------------------------------------')
# 设置TIM为活动窗口
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(win32gui.FindWindow("TXGuiFoundation","TIM"))
# 查看是否修改
print_GetForegroundWindow()
# 加上显示到最前端,这里使用 SW_SHOW,用参考链接中得max有时会导致窗体不可点击
win32gui.ShowWindow(win32gui.FindWindow("TXGuiFoundation","TIM"), win32con.SW_SHOW)
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有