According to this answer on another question, the Collapsed event of a ContextMenu is only raised if it’s associated to a control before calling Show().
SInce a NotifyIcon does not count as a control, I can’t hook onto the Collapsed event to detect when the menu associated to one is hidden.
Are there any workarounds?
Under the “Remarks” section for the MSDN docs on TrackPopupMenuEx, it says:
So that could mean that when the
ContextMenuis visible, the window on theNotifyIconwill be the foreground window. You can see from by looking atNotifyIcon.ShowContextMenu()that it is indeed the case:Using ILSpy I then noticed
NotifyIconhas a private memberwindow, which refers to a private class with base typeNativeWindow. Therefore, you can check like this: