Does NotifyIcon have a MouseDown equivalent?
Asked Answered
L

2

7

I have a NotifyIcon in the system tray. How can I detect when the user has left-clicked down on it? I assumed the MouseDown event would be what I want to use but it only handles right click and middle-button click. For left-click, it only fires after the user has let go (as in they've just performed a normal click). Is there a way to get the MouseDown event only?

Legion answered 29/2, 2012 at 22:39 Comment(3)
I tried several things too. You have me curious. I even installed VS11 to try it there as well. No luck. I almost think its a bug. I hope someone has an answer too.Burnight
I think it may be something to do with the fact that it can be dragged (which is handled by Windows itself rather than the app). Maybe the mousedown event is being absorbed by Windows instead of bubbling up to the app.Legion
So this on MSDN is totally inaccurate?Crawly
C
5

This is by design, the shell synthesizes the MouseDown message from the up event. You'll see why it works this way when you click and hold the button down and then start dragging. Note how the notification area overflow window pops up and lets you drag the icon into it to remove it from the visible area. It can't work both ways.

Technically you could hook the window owned by Explorer.exe to get a crack at the messages before Explorer does with SetWindowsHookEx(). That however requires a kind of DLL that you cannot write in C#, it needs to be injected into Explorer. Very destabilizing and hard to beat the competition that is trying to do the same thing. Also the kind of code that causes sleepless nights for the Microsoft appcompat team.

Cambric answered 4/3, 2012 at 18:42 Comment(0)
V
3

It appears that the underlying Win32 API Shell_NotifyIcon sends a WM_LBUTTONDOWN message when the user clicks the icon. According to MSDN anyway.

Examining the Windows Forms source code for NotifyIcon reveals standard mouse down event handling, so if the Win32 message was being sent at the "correct" time it would work as you want/expect.

I have to agree with a previous comment that NotifyIcon will be swallowing WM_LBUTTONDOWN since it needs to do mouse capture to allow the user to drag the icons about.

It's possible that this article about creating a tray icon for WPF will be useful since it shows how to use SetWindowsHookEx etc from C#.

Valdovinos answered 4/3, 2012 at 14:28 Comment(2)
Does that mean that there's no way of handling the mousedown event since it's by design?Legion
I don't think there's any easy way. It may be possible to hook WM_XXX messages at a low level (in C++) before the taskbar handles them, but it's a long time since I've done anything like that.Valdovinos

© 2022 - 2024 — McMap. All rights reserved.