I was looking at the documentation for TrackPopupMenuEx() today and saw this in the remarks section. This, I suspect, explains a lot of frustration I have experienced in the past.
To display a context menu for a notification icon, the current window must be the foreground window before the application calls TrackPopupMenu or TrackPopupMenuEx. Otherwise, the menu will not disappear when the user clicks outside of the menu or the window that created the menu (if it is visible).
So, if you own an app that does this, please fix it. The World and I thank you for it.