使用 win32com.client 的 AppActivate 根据 ID 将焦点设置到窗口
我已尝试以下操作,但焦点没有返回到运行脚本时具有焦点的程序:
import win32com.client
import win32gui
current = win32gui.GetForegroundWindow()
shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate('Console2')
shell.SendKeys('{UP}{ENTER}')
shell.AppActivate(str(current))
I've tried the following, but focus isn't returned to the program that had focus when the script was run:
import win32com.client
import win32gui
current = win32gui.GetForegroundWindow()
shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate('Console2')
shell.SendKeys('{UP}{ENTER}')
shell.AppActivate(str(current))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,
win32gui.GetForegroundWindow()
返回的是窗口句柄,而不是进程 ID。win32process.GetWindowThreadProcessId(hwnd)
可用于从句柄中获取线程 ID 和进程 ID。It turns out that
win32gui.GetForegroundWindow()
returns the window handle and not the process ID.win32process.GetWindowThreadProcessId(hwnd)
can be used to get the thread ID and process ID from the handle.没有足够的代表对此发表评论
除了 Acorn 的回答(很久以前)之外,您现在应该能够使用 SetFocus(handle)。
资料来源: http://timgolden.me.uk/pywin32-docs/win32gui__SetFocus_meth.html< /a>
Not enough rep to comment this
In addition to Acorn's answer (so long ago), you should now be able to use SetFocus(handle).
Source: http://timgolden.me.uk/pywin32-docs/win32gui__SetFocus_meth.html