问题描述
我正在使用PInvoke在.NET中使用SetForegroundWindow API.
I'm using SetForegroundWindow API in .NET using PInvoke.
当我在Visual Studio中调试时使用API时,它可以完美运行.但是,当应用程序正常运行时,它并不总是能正常工作.
When I use the API while debugging in Visual Studio its works perfectly. But it doesn't work always when the application is running normally.
我在调用SetForegroundWindow之前放了一些日志,它确认该API正在被调用,但有时不起作用.我也看过几篇有关此问题的文章,但我想知道为什么它失败了.
I put some logs just before a call to SetForegroundWindow and its confirmed that the API is getting called but doesn't take effect at times. I have also seen a couple of posts regarding this issue but I want to know why is it failing.
帖子的链接如下:
- Win32 SetForegroundWindow unreliable
- SetForegroundWindow only working while visual studio is open
推荐答案
David是正确的,并把我引向了正确的方向.跟着代码项目的文章说 如果用户按下ALT键或采取某些措施导致系统本身更改前景窗口,则系统会自动启用对SetForegroundWindow的调用"
David was right and led me to the right direction . Followed the code project article which says "The system automatically enables calls to SetForegroundWindow if the user presses the ALT key or takes some action that causes the system itself to change the foreground window"
如何将窗口显示在顶部使用SetForegroundWindow()
这篇关于NET中的SetForegroundWindow问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!