简体   繁体   English

将窗口置于前台的问题

[英]Problems with bringing window to foreground

I have an app that user doesn't actually close - instead of shutting down whole application, the main window is hidden. 我有一个用户实际上没有关闭的应用程序-隐藏主窗口而不是关闭整个应用程序。 Then when user again launches the same application, the second instance communicates with the first (already running) instance and first instance tries to bring the main app window to the foreground. 然后,当用户再次启动同一应用程序时,第二个实例与第一个(已运行)实例进行通信,第一个实例尝试将主应用程序窗口置于前台。

The problem is that this mostly succeeds, but sometimes fails. 问题在于,这种方法大多数情况下都会成功,但有时会失败。 I've done my research and it seems that background processes can not bring their windows to the foreground, which is somewhat reasonable. 我已经完成了研究,似乎后台进程无法将其窗口置于前台,这在一定程度上是合理的。 The solution is to call AllowSetForegroundWindow from second instance (which can bring a window to the foreground because the user has started it) and give permission to first instance of the program. 解决方案是从第二个实例调用AllowSetForegroundWindow(由于用户已启动它,因此可以将窗口带到前台),并授予该程序的第一个实例的权限。

My question is: how can I make sure that my code works? 我的问题是:如何确保我的代码能正常工作? I'm asking this because I didn't find a way to check if AllowSetForegroundWindow call had any effect. 我之所以这样问是因为我没有找到一种方法来检查AllowSetForegroundWindow调用是否有效。 I don't know when first instance won't be able to set foreground window (because it mostly works and rarely doesn't), so I don't know how to check if everything is working properly. 我不知道什么时候第一个实例将无法设置前景窗口(因为它大多数都可以工作,而很少不能),所以我不知道如何检查一切是否正常。 Any ides? 有想法吗?

AllowSetForegroundWindow() has a return code which you can check: AllowSetForegroundWindow()具有返回码,您可以检查该返回码:

The function will fail if the calling process cannot set the foreground window. 如果调用进程无法设置前台窗口,则该功能将失败。

When your app is launched it should be able to set the foreground window, and so therefore AllowSetForegroundWindow() should succeed, and your existing app's instance should be able to come to the front. 启动应用程序时,它应该能够设置前景窗口,因此AllowSetForegroundWindow() 应该成功,并且现有应用程序的实例应该能够位于最前面。

However there's no way to guarantee that this will work all the time. 但是,没有办法保证这将一直有效。 All you can do is implement the documented system as well as you can. 您所能做的就是尽可能地实施文档化的系统。 In the very worst case, your existing app opens behind other windows and the user has to click it to bring it to the front. 在最坏的情况下,您现有的应用会在其他窗口后面打开,用户必须单击它才能将其置于最前面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM