如何从 Powershell 事件订阅者操作设置前台窗口
我有一个 FileSystemWatcher 实例在我的 PoSh 会话后台运行,监视文本文件的更改。 PoSh 事件订阅者附加到此事件,并且在触发时,它通过调用 Start-Process 启动控制台程序。该程序从当前前台窗口(我的 PoSh 控制台)窃取焦点。从 PoSh 事件订阅者调用 SetForegroundWindow 将焦点返回到我的 PoSh 控制台不起作用。 SwitchToThisWindow 在大部分时间确实有效,但根据 MSDN 文档,不应使用它。
在这种情况下,我可以防止 Start-Process 窃取焦点,或者将其从事件订阅者设置回在该事件触发之前拥有焦点的窗口吗?
I have a FileSystemWatcher instance running in the background of my PoSh session watching for changes to text files. A PoSh event subscriber is attached to this event and, when fired, it launches a console program by calling Start-Process. This program steals de focus from the current foreground window (my PoSh console). Calling SetForegroundWindow from the PoSh event subscriber to return the focus to my PoSh console doesn't work. SwitchToThisWindow does work most of the time, but according to the MSDN docs, it shoulnd't be used.
Can I prevent Start-Process from stealing the focus in this situation or set it back from the event subscriber to the window that had it before this event is fired?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对我来说
SetForegroundWindow
效果很好。检查此代码:但请注意,如果您托管 PowerShell 或使用例如控制台 (http://sourceforge.net/projects/ console/) 那么 MainWindowHandle 就是你的主机程序的句柄。
因此,您需要的不是
(Get-Process -id $pid).MainWindowHandle
,而是需要[tricks]::SetForegroundWindow((Get-Process console).MainWindowHandle)
。计时器事件示例:
否则,如果您运行隐藏窗口的进程,它可以解决您的问题。
从 msdn 上有关 Process 类的文档中获取并更改的示例
For me
SetForegroundWindow
works well. Check this code:But note that if you host PowerShell or use e.g. Console (http://sourceforge.net/projects/console/) then MainWindowHandle is the handle of your host program.
So instead of
(Get-Process -id $pid).MainWindowHandle
you would need[tricks]::SetForegroundWindow((Get-Process console).MainWindowHandle)
.Example with timer event:
Otherwise if you run process that has its window hidden, it could solve your problem.
Example taken and altered from documentation on msdn about Process class
听起来它不起作用,因为你在失去焦点之前就设置了焦点。
您是否尝试过通过工作来集中注意力?当您使用控制台时,它在后台运行。
像这样的东西可能会起作用,它会在事件发生后让你的焦点保持10秒
如果你混合在GetForegroundWindow中,你可以等到你失去焦点,然后把它抓回来
http://www.leeholmes.com/blog/MorePInvokeInPowerShell.aspx
It sounds like it didnt work because you set focus, before you lost focus.
Have you tried setting focus through a job? It runs in the background while you use the console.
Something like this might work, it keeps your focus for 10 seconds after the event
If you mix in GetForegroundWindow, you can wait till you lose focus, then grab it back
http://www.leeholmes.com/blog/MorePInvokeInPowerShell.aspx
当我发现这个问题时,我从上面的 @stej 答案中得到提示,因为我试图做同样的事情,我扩展以生成此代码,这将使脚本重新成为焦点,无论是在 ISE、控制台窗口还是通过cmd 提示符(通过批处理文件)。
或者为了更容易地重复使用,请将以下内容保存为模块目录中的 .psm1 文件 - 从 PS v3 开始,您不必导入它,调用模块目录中模块中的函数即可导入它。
要手动导入,
Import-Module .\Getfocus.psm1
(假设它位于您当前的路径中)。Taking my cue from @stej answer above when I found this question because I was trying to do the same thing, I expanded to produce this code, which will bring the script back into focus whether being run in the ISE, console window, or via cmd prompt (through a batch file).
Or to re-use more readily, save the following as a .psm1 file in your module directory - from PS v3 onwards, you don't have to import it, calling a function in a module in your module directory imports it.
To import manually,
Import-Module .\Getfocus.psm1
(Assuming it's in your current path).