2011-08-30 47 views
0

當窗體關閉時,我試圖顯示NotifyIcon。它關閉了,但當我點擊最小化按鈕時它也會關閉。這是我的代碼。當使用NotifyIcon時,WindowsState最小化不起作用

private void Home_Resize(object sender, EventArgs e) 
    { 

     if (FormWindowState.Minimized == this.WindowState) 
     { 
      notifyIcon1.Visible = true; 
      notifyIcon1.ShowBalloonTip(500); 
      this.Hide(); 
     } 

     else if (FormWindowState.Normal == this.WindowState) 
     { 
      notifyIcon1.Visible = true; 
     } 
    } 

    private void notifyIcon1_DoubleClick(object sender, EventArgs e) 
    { 
     if (this.WindowState == FormWindowState.Minimized) 
     { 
      this.Show(); 
      this.Activate(); 
      this.WindowState = FormWindowState.Normal; 

     } 
    } 

    private void Home_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     e.Cancel = true; 
     this.WindowState = FormWindowState.Minimized;   
    } 

    private void toolStripMenuItem1_Click_1(object sender, EventArgs e) 
    { 
     //Exit App 
     notifyIcon1.Visible = false; 
     Environment.Exit(0); 
    } 
+0

當您最小化表單時隱藏表單,因此它並未實際關閉。 –

+0

我覺得Resize事件不好檢查windowstate。 – Reniuz

+0

如果是的話我可以使用哪種事件?謝謝:) – novasaint

回答

1

只需將代碼從Resize事件處理程序移動到FormClosing事件處理程序。同時檢查e.CloseReason,當Windows關閉時,表單需要關閉。

+0

謝謝:))現在好了 – novasaint

相關問題