简体   繁体   English

自定义任务栏图标通知气球

[英]Custom tray icon notification balloon

I have a C# program that sits in the system tray and pops up a notification balloon now and then. 我有一个C#程序,它位于系统托盘中,并不时弹出一个提示气球。 I'd like to provide 2-3 buttons on the notification balloon to allow the user to take various actions when the notification appears - rather than, for example, having to click the notification balloon to display a form containing buttons for each possible action. 我想在通知提示框上提供2-3个按钮以允许用户在通知出现时采取各种操作-而不是例如不必单击通知提示框以显示包含每个可能操作的按钮的表单。

I'm looking for suggestions on the best way to go about implementing this. 我正在寻找实现此目标的最佳方法的建议。

Edit: clarification, I want to provide buttons on the notification balloon so the user can take direct action on the notification rather than having to take action through some other part of the application (a form or menu for example). 编辑:澄清,我想在通知气球上提供按钮,以便用户可以对通知采取直接操作,而不必通过应用程序的其他部分(例如表单或菜单)进行操作。

There's no built-in method for this. 没有内置方法。 I would suggest writing your own "balloon" and activating that instead of calling .ShowBalloon() 我建议您编写自己的“气球”并激活它,而不是调用.ShowBalloon()

This is how I do it. 这就是我的方法。 It may not be the correct way of doing it. 这可能不是正确的方法。 I do this way because .ShowBalloonTip(i) doesn't work as expected for me. 我这样做是因为.ShowBalloonTip(i)无法按我的预期工作。 It doesn't stay for i seconds and go off. 它不会停留i几秒钟,然后熄灭。 So I do in another thread and forcefully dispose off. 因此,我在另一个线程中执行并强制处理。

    private static NotifyIcon _notifyIcon;

    //you can call this public function
    internal static void ShowBalloonTip(Icon icon)
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.RunWorkerAsync(icon);
    }

    private static void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        Show(e);
        Thread.Sleep(2000); //meaning it displays for 2 seconds
        DisposeOff();
    }

    private static void Show(DoWorkEventArgs e)
    {
        _notifyIcon = new NotifyIcon();
        _notifyIcon.Icon = (Icon)e.Argument;

         _notifyIcon.BalloonTipTitle = "Environment file is opened";
        _notifyIcon.BalloonTipText = "Press alt+tab to switch between environment files";

        _notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
        _notifyIcon.Visible = true;
        _notifyIcon.ShowBalloonTip(2000); //sadly doesnt work for me :(
    }

    private static void DisposeOff()
    {
        if (_notifyIcon == null)
            return;

        _notifyIcon.Dispose();
        _notifyIcon = null;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM