2013-04-22 76 views
0

我仍然無法使其工作,通知欄中不顯示任何內容。這是一個完整的代碼,以儘量減少,到目前爲止:應用程序不會最小化到通知欄

private void button6_Click(object sender, EventArgs e) 
{ 
    this.WindowState = FormWindowState.Minimized; 
} 

private void Form_Resize(object sender, EventArgs e) 
{ 
    if (WindowState == FormWindowState.Minimized) 
    { 
     this.Hide(); 
    } 
} 

private void notifyIcon_Click(object sender, EventArgs e) 
{ 
    this.Show(); 
    this.WindowState = FormWindowState.Normal; 
} 

爲什麼不是這方面的工作?

+0

你正在嘗試做什麼? – Mogli 2013-04-22 07:52:13

+0

他正試圖隱藏應用程序,在系統托盤 – Pyromancer 2013-04-22 07:53:46

+0

是的,我需要的應用上按下按鈕,只顯示disapear在系統托盤中 – Connor 2013-04-22 07:54:35

回答

4

你從來沒有顯示什麼在通知區域。跟蹤代碼並嘗試查看發生了什麼。我已添加一些評論:

private void button6_Click(object sender, EventArgs e) 
{ 
    // When button 6 is clicked, minimize the form. 
    this.WindowState = FormWindowState.Minimized; 
} 

private void Form_Resize(object sender, EventArgs e) 
{ 
    // Catch the case where the form is minimized, including but not limited to 
    // clicks on button 6. 
    if (WindowState == FormWindowState.Minimized) 
    { 
     // In that case, hide the form. 
     this.Hide(); 
    } 
} 

private void notifyIcon_Click(object sender, EventArgs e) 
{ 
    // If the notification icon is clicked, reshow the form as a normal window. 
    this.Show(); 
    this.WindowState = FormWindowState.Normal; 
} 

現在注意問題所在?當表格被最小化時,你所做的就是隱藏它。你永遠不會告訴NotifyIcon顯示其圖標!其Visible property的默認值是false。您必須將其設置爲true以使圖標顯示出來,並且false可以讓它消失。

所以修改代碼如下:

private void Form_Resize(object sender, EventArgs e) 
{ 
    // Catch the case where the form is minimized, including but not limited to 
    // clicks on button 6. 
    if (WindowState == FormWindowState.Minimized) 
    { 
     // In that case, hide the form. 
     this.Hide(); 

     // And display the notification icon. 
     notifyIcon.Visible = true; 

     // TODO: You might also want to set other properties on the 
     // notification icon, like Text and/or Icon. 
    } 
} 

private void notifyIcon_Click(object sender, EventArgs e) 
{ 
    // If the notification icon is clicked, reshow the form as a normal window. 
    this.Show(); 
    this.WindowState = FormWindowState.Normal; 

    // And hide the icon in the notification area. 
    notifyIcon.Visible = false; 
} 
+0

感謝,但IM仍然得到錯誤:該名稱的NotifyIcon不會在目前情況下 – Connor 2013-04-22 08:03:31

+1

那麼又是什麼您的通知圖標控件調用存在嗎?我認爲它是'notifyIcon',因爲'Click'事件處理程序方法的名稱是'notifyIcon_Click'。它可能類似於'notifyIcon1'。檢查設計師將其添加到表單中的內容並查看它的名稱。 @connor – 2013-04-22 08:04:12

+0

啊,可能是我的問題,我沒有控制.....我怎麼做嗎? – Connor 2013-04-22 08:05:59

1

試試這個代碼:

private NotifyIcon notifyIcon; 
public Form1() 
{ 
    InitializeComponent(); 

    button6.Click += (sender, e) => 
     { 
      this.WindowState = FormWindowState.Minimized; 
     }; 

    this.Resize += (sender, e) => 
     { 
      if (WindowState == FormWindowState.Minimized) 
      { 
       this.Hide(); 
       notifyIcon.Visible = true; 
       notifyIcon.ShowBalloonTip(1000); 
      } 
     }; 

    notifyIcon = new NotifyIcon() 
     { 
      Text = "I'm here!", 
      BalloonTipText = "I'm here!", 
      Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath) 
     }; 

    notifyIcon.Click += (sender, e) => 
     { 
      this.Show(); 
      this.WindowState = FormWindowState.Normal; 
      notifyIcon.Visible = false; 
     }; 
} 
相關問題