C# 系统托盘通知图标赢得';不接受左键单击事件

C# 系统托盘通知图标赢得';不接受左键单击事件,c#,windows,winforms,click,C#,Windows,Winforms,Click,我正在创建一个仅限系统托盘的应用程序。没有主窗体的图标有点复杂,但是通过前面关于StackOverflow的主题,我已经解决了这个问题。右键点击效果很好,我在上下文菜单中链接过,等等 我左键点击有问题。据我所知,“notifyIcon1_Click”事件根本没有触发 private void notifyIcon1_Click(object sender, EventArgs e) { Debug.WriteLine("Does it work here?");

我正在创建一个仅限系统托盘的应用程序。没有主窗体的图标有点复杂,但是通过前面关于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!");
        }
    }
这些调试行都没有输出,该事件中的断点不会停止程序,等等


我做得不对吗?我的下一步应该是什么?我正在用C#编写此代码,如果这对任务栏行为有任何影响,请使用Windows 7。

如果要确定是左键单击还是右键单击,请连接
鼠标单击,而不是单击

这样你就可以得到这样的签名:

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

另一个答案不清楚是否需要鼠标单击事件而不是单击

notifyIcon.MouseClick += MyClickHandler;
那么你的处理函数就可以正常工作了

void MyClickHandler(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        Console.WriteLine("Left click!");
    }
}

如果需要消息/引出序号本身的单击事件,请使用

_notifyIcon.BalloonTipClicked += notifyIconBalloon_Click;


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

你能展示更多你的代码吗?事件是否“已连接”?事件是否已在Designer.cs文件中正确连接-
this.notifyIcon1.Click+=new System.EventHandler(this.notifyIcon1\u Click)?您确实记得将其注册为事件处理程序,对吗?谢谢大家:0我非常习惯于它为我自动连接它们,有时会像这样溜过去。。。这和互联网上的其他人都有类似的问题,这似乎是一个更大的问题。诸如此类。在这种情况下,“this.notifyIcon1.Click+=new System.EventHandler(this.notifyIcon1_Click);”的等价物是什么?显而易见(即,将“Click”替换为“MouseClick”会引发“notifyIcon1\u MouseClick”的无重载与委托“System.EventHandler”匹配”错误。除此之外,谢谢!我正要开始解释为什么我不能左/右工作,但你打败了我。*编辑,试图获得上面的评论者使用的代码块。但它不起作用。希望评论有一个功能齐全的文本编辑器…嘿@cksubs,如果你能展示一下你所做的,那将非常有用为了解决这个问题,这将有助于像我这样的未来游客。;)谢谢!在下面发布了完整的解决方案。
    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();
        }
    }