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.
source share