Restoring a window back to the previous owner

I have an application that is in the system tray, which, when double-clicked, opens a window that is pretty standard; however, when you close the window, I would like the window to be focused before mine was open to get focus.

If I pulled out my window using a keyboard shortcut, I can restore the previous focus on closing using the GetForegroundWindow API GetForegroundWindow before my window opens, then the SetForegroundWindow method after I close the window (with the value of the first call) to restore focus. This does not work when you open the window through the system tray, because the user has essentially focused on the system tray.

I tried using a combination of GetForegroundWindow , GetWindow and GetTopMostWindow to try moving to the z-order to find the second window after the taskbar (assuming that the system tray will jump to the top, so that logically the next shot will be the original front). I did not have any success, although the results of these functions are pretty useless, since they do not seem to give me any logical structure.

Does anyone have any ideas on how I could achieve this?

I thought of some kind of background observer that just sits and controls what the front window is and keeps a pointer to it, but at best it will be flaky.

This is on Windows (I personally am on x64 Server 2008) and with .Net 3.5.

+4
source share
3 answers

Although this may be a nice usability feature, you probably know that with Windows 7 the tray will get (much less) desktop space.

The only way to do this is to keep track of Windows messages and keep track of which window was centered before your application got the focus. Other than this AFAIK, you cannot do much.

+2
source

I looked at all my system tray icons and they have the same behavior as your program. If you want Windows focus control to be hairy, you can think twice (or more) before trying to change the default behavior.

+1
source

I did this to return to the previous window

 SendKeys.Send("%{TAB}"); 

I know that this is not a "decision", but to some extent serves the purpose.

+1
source

All Articles