系统托盘notifyIcon不接受左键单击事件

问题描述 投票:3回答:2

我正在创建一个仅限System-Tray的应用程序。拥有没有主窗体的图标有点复杂,但是通过StackOverflow上的先前主题我已经解决了。右键单击工作正常,我已在上下文菜单中链接等。

我在左键单击时遇到问题。据我所知,“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!");
        }
    }

这些调试行都没有输出,该事件中的断点不会停止程序等。

我做错了吗?我的下一步应该是什么?我正在使用Windows 7在C#中对此进行编码,如果这对任务栏行为完全重要的话。

c# windows winforms click
2个回答
7
投票

如果要确定是左击还是右击,请将MouseClick连接起来,而不是单击。

这样你会得到这样的签名:

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 :)
}

0
投票

如果您希望使用Message / Balloon本身的click事件

_notifyIcon.BalloonTipClicked += notifyIconBalloon_Click;


private void notifyIconBalloon_Click(object sender, EventArgs e)
{
// your code
}

-1
投票

另一个答案并不清楚您需要MouseClick事件而不是Click。

notifyIcon.MouseClick += MyClickHandler;

然后你的处理函数将正常工作。

void MyClickHandler(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        Console.WriteLine("Left click!");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.