Fix broken focus when starting certain applications such as Microsoft Word and Excel.
It seems that GetForegroundWindow isn't updated for a short while after the foreground event in these cases as well. Therefore, cache the window from the foreground event and use that instead of calling GetForegroundWindow within a short timeout.
We didn't notice this in the past because we were handling a lot of pointless events that we aren't handling now.
Re #4001.