windows 系统托盘通知图标不会接受左键单击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2045462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System Tray notifyIcon won't accept left-click event
提问by ck_
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.
我正在创建一个仅限系统托盘的应用程序。没有主窗体的图标有点复杂,但通过以前关于 StackOverflow 的主题,我已经解决了。右键单击工作正常,我已链接到上下文菜单等。
I'm having problems with the left-click. As far as I can tell, the "notifyIcon1_Click" event isn't firing at all.
我在左键单击时遇到问题。据我所知,“notifyIcon1_Click”事件根本没有触发。
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.
我这样做不正确吗?我的下一步应该是什么?我正在用 C# 编写代码,如果这对任务栏行为很重要,则使用 Windows 7。
回答by Tristan Warner-Smith
If you want to determine if it's a left or right click, wire up the MouseClick
, rather than click.
如果要确定是左键单击还是右键单击,请连接MouseClick
,而不是单击。
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 :)
}
回答by Wowo Ot
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
}
回答by Simon Perepelitsa
The other answer is not clear that you need MouseClick event instead of Click.
另一个答案不清楚您需要 MouseClick 事件而不是 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!");
}
}