SetForegroundWindow ought to work because when the tray icon was right-clicked, the focus transferred to that icon's owner hWnd (set by NOTIFYICONDATA.hWnd). It is a known bug with tray icons and context menus. If SetForegroundWindow isn't called, the context menu is left open even when clicking other windows. SetForegroundWindow fixes that. Google other examples of tray icons and you'll see what I mean.