气球提示关闭后,C#NotifyIcon保持在托盘中

时间:2012-10-19 11:22:19

标签: c# notifyicon

在NotifyIcon的Ballon Tip关闭后,图标仍保留在托盘中。 只有当我用鼠标光标悬停它时它才会消失。

我有一个Notification.cs类,它有一个组件 - NotifyIcon。

我从我的控制台应用程序中调用它,因为我只想在满足某些条件时显示通知。

一些代码:

  • 我如何在我的程序中调用通知:

    Notification not = new Notification("Error occured, do this or that", "Some error", System.Windows.Forms.ToolTipIcon.Error);
    not.getIcon().ShowBalloonTip(1000);
    
  • 通知类:

    public Notification(string baloonTipText, string baloonTipTitle, System.Windows.Forms.ToolTipIcon icon) : this()
    {
        this.icon.Visible = true;
        this.icon.BalloonTipText = baloonTipText;
        this.icon.BalloonTipTitle = baloonTipTitle;
        this.icon.BalloonTipIcon = icon;
    }
    
    public System.Windows.Forms.NotifyIcon getIcon()
    {
        return this.icon;
    }
    
    private void icon_BalloonTipClosed(object sender, EventArgs e)
    {
        this.icon.Visible = false;
        this.icon.Dispose();            
    }
    

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

因为您在控制台应用程序中运行,所以当气球提示关闭时(无消息泵),将不会调用icon_BalloonTipClosed处理程序。您需要在应用程序退出时手动调用Dispose,或者设置计时器(System.Threading.TimerSystem.Windows.Forms.Timer将无效),超时时间超过气球提示超时。 e.g:

timer = new Timer(state => not.getIcon().Dispose(), null, 1200, Timeout.Infinite);

答案 1 :(得分:0)

using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace ShowToolTip
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btBallonToolTip_Click(object sender, EventArgs e)
        {
            ShowBalloonTip();
            this.Hide();
        }

        private void ShowBalloonTip()
        {
            Container bpcomponents = new Container();
            ContextMenu contextMenu1 = new ContextMenu();

            MenuItem runMenu = new MenuItem();
            runMenu.Index = 1;
            runMenu.Text = "Run...";
            runMenu.Click += new EventHandler(runMenu_Click);

            MenuItem breakMenu = new MenuItem();
            breakMenu.Index = 2;
            breakMenu.Text = "-------------";

            MenuItem exitMenu = new MenuItem();
            exitMenu.Index = 3;
            exitMenu.Text = "E&xit";

            exitMenu.Click += new EventHandler(exitMenu_Click);

            // Initialize contextMenu1
            contextMenu1.MenuItems.AddRange(
                        new System.Windows.Forms.MenuItem[] { runMenu, breakMenu, exitMenu });

            // Initialize menuItem1

            this.ClientSize = new System.Drawing.Size(0, 0);
            this.Text = "Ballon Tootip Example";

            // Create the NotifyIcon.
            NotifyIcon notifyIcon = new NotifyIcon(bpcomponents);

            // The Icon property sets the icon that will appear
            // in the systray for this application.
            string iconPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\setup-icon.ico";
            notifyIcon.Icon = new Icon(iconPath);

            // The ContextMenu property sets the menu that will
            // appear when the systray icon is right clicked.
            notifyIcon.ContextMenu = contextMenu1;

            notifyIcon.Visible = true;

            // The Text property sets the text that will be displayed,
            // in a tooltip, when the mouse hovers over the systray icon.
            notifyIcon.Text = "Morgan Tech Space BallonTip Running...";
            notifyIcon.BalloonTipText = "Morgan Tech Space BallonTip Running...";
            notifyIcon.BalloonTipTitle = "Morgan Tech Space";
            notifyIcon.ShowBalloonTip(1000);
        }

        void exitMenu_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void runMenu_Click(object sender, EventArgs e)
        {
            MessageBox.Show("BallonTip is Running....");
        }
    }
}