System Tray notifyIcon won't accept left-click event
I'm creating a System-Tray only application. It's somewhat complicated to have the icon without a main form, but through previous topics on StackOverflow I've worked it out. The right-click works fine, I've linked in a context menu, etc.
I'm having problems with the left-click. As far as I can tell, the "notifyIcon1_Click" event isn't firi开发者_StackOverflow社区ng at all.
private void notifyIcon1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Does it work here?");
if (e.Equals(MouseButtons.Left))
{
Debug.WriteLine("It worked!");
}
}
Neither of those debug lines are outputting, breakpoints in that event don't stop the program, etc.
Am I doing this incorrectly? What should my next step be? I'm coding this in C#, using Windows 7 if that matters at all for taskbar behavior.
If you want to determine if it's a left or right click, wire up the MouseClick
, rather than click.
That way you get a signature like this:
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
//Do the awesome left clickness
else if (e.Button == MouseButtons.Right)
//Do the wickedy right clickness
else
//Some other button from the enum :)
}
If you want the click event of the Message/Balloon itself use
_notifyIcon.BalloonTipClicked += notifyIconBalloon_Click;
private void notifyIconBalloon_Click(object sender, EventArgs e)
{
// your code
}
private void NotifyIcon_Click(object sender, EventArgs e)
{
MouseEventArgs mouseEventArgs = (MouseEventArgs)e;
if (mouseEventArgs.Button == MouseButtons.Right && IsHandleCreated)
{
popupMenu1.ShowPopup(MousePosition);
return;
}
if (mouseEventArgs.Button == MouseButtons.Left && IsHandleCreated)
{
if (isWindowMinimized)
showWindow();
else
hideWindow();
}
}
The other answer is not clear that you need MouseClick event instead of Click.
notifyIcon.MouseClick += MyClickHandler;
Then your handler function will work fine.
void MyClickHandler(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Console.WriteLine("Left click!");
}
}
精彩评论