2012-09-14 56 views
8

我對C#編程非常陌生,儘管我已經在unity3D中編寫了C#腳本幾年。 我目前想做一個WPF托盤圖標,所有我在網上找到的消息來源告訴我使用使用wpf的C#trayicon

System.Windows.Forms 

然而者,恕不不是System.Windows爲我用,我有不知道爲什麼不。誰能幫我這個?

回答

25

您需要添加到System.Window.Forms和System.Drawing中集的引用,然後你使用它像這樣。假設您嘗試最小化Window to tray圖標並在用戶單擊該圖標時再次顯示它:

public partial class Window : System.Windows.Window 
{ 

    public Window() 
    { 
     InitializeComponent(); 

     System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon(); 
     ni.Icon = new System.Drawing.Icon("Main.ico"); 
     ni.Visible = true; 
     ni.DoubleClick += 
      delegate(object sender, EventArgs args) 
      { 
       this.Show(); 
       this.WindowState = WindowState.Normal; 
      }; 
    } 

    protected override void OnStateChanged(EventArgs e) 
    { 
     if (WindowState == WindowState.Minimized) 
      this.Hide(); 

     base.OnStateChanged(e); 
    } 
} 
+0

優秀的單詞引用幫助我瞭解如何以及在哪裏。 我現在有一個托盤圖標,謝謝=) – Logan

+2

如果你已經在System.Windows引用的窗口中找到它,你將遇到一些模糊問題。我使用WinForms = System.Windows.Forms來添加一個名字來解決它:然後用WinForms.NotifyIcon notifyIcon = new WinForms.NotifyIcon()來調用它。 –