简体   繁体   中英

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.

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. 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:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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