Alternatively show / hide the window when you click the notification icon

I am implementing a WPF-style Windows 7 / Vista pop-up application ("system tray"). I wrote about my work so far here (definition of icon icon icon, disabling resizing, etc.).

There is one problem that I have not solved, to my satisfaction, however: hiding the window when the notification icon is clicked a second time. If you click (for example) the volume icon in Vista / 7 to display the volume control, note that it is hidden again when the icon is double-clicked.

I am processing the Deactivated Event window to hide the window, and the window is really deactivated when the notification icon is clicked. However, clicking the notification icon certainly shows and activates the window, so all that happens is that the window disappears when the mouse is turned off and reappears when the mouse is released (completion of the mouse click event).

My first thought was that I could use the MouseDown notification icon (I use System.Windows.Forms.NotifyIcon) and check if this window is visible at this time - if that were the case, I could interpret it as a user double-click the notification icon to hide the window. Unfortunately, the MouseDown event does not seem to fire until the mouse is clicked (in other words, it works the same as the MouseClick event), and by this time the window has already been deactivated and thus is hidden. This seems to rule out this solution.

My next idea (and the approach I used) was to get the cursor position when the window is deactivated (GetCursorPos) and check if this point is within the borders of the notification icon. At the same time, I also use GetForegroundWindow to find the currently active window - if the notification icon really needs to be clicked, it should either be a taskbar (top-level window with the class name Shell_TrayWnd) or a crash from the notification area (top-level window with the class name NotifyIconOverflowWindow; Windows 7+ only). In short, if the cursor is over the notification icon and the notification area is active, I assume that the mouse user knocked down the notification icon to hide the window. If these conditions are true, then the next MouseClick event will not cause the window to appear / activate.

: , Windows, "" ( "Windows + " ), ( ). , , , . ( .)

, , , ; , .

, - , .

, : , Windows 7 . () , 2 . , , , .

+3
1

, . , , . . Win32, , .

. WPF. , IInputElement, .

+1

Source: https://habr.com/ru/post/1783234/


All Articles