2012-10-19 117 views
1

在我的NotifyIcon的氣球提示已關閉後,圖標仍停留在托盤中。 只有當我用鼠標光標將其懸停時,它纔會消失。C#NotifyIcon在氣球提示已關閉後停留在托盤中

我有一個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();    
    } 
    

任何想法?

+0

我問[同樣的問題](http://stackoverflow.com/questions/10980029/notify-icon-stays-in-system-tray-on-application-close)一而之前,沒有得到答案,但一些有用的意見 – JMK

+0

@JMK我不太確定這是同一個問題。我讀到這個問題的方式,該程序仍在運行。 – hvd

+0

什麼'type'是'Notification'類? –

回答

2

因爲您正在控制檯應用程序中運行,所以當氣球提示關閉時(無消息泵),將不會調用您的icon_BalloonTipClosed處理程序。您需要手動撥打Dispose,因爲您的應用程序退出或設置了一個計時器(System.Threading.Timer,System.Windows.Forms.Timer將不起作用),並且超時時間超過氣球提示的超時時間。例如:

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

在週末後嘗試的類,然後會告訴它它是否工作。謝謝! – wojtuch

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...."); 
     } 
    } 
}