2015-10-13 108 views
3

我在.Net中建立了托盤應用程序,它工作正常。但是,用戶希望在某些情況下在運行時更改托盤圖標圖像。爲了簡單起見,讓我們說,有些東西不起作用 - 托盤圖標應該顯示紅色圖像;如果一切都很好,它應該顯示綠色。我不知道如何在.Net中實現這一點。更改系統托盤圖標圖像

請提供一些輸入。謝謝

我構建了托盤CustomApplicationContent。下面的一些片段:

的Program.cs

[STAThread] 
    static void Main() 
    { 
     if (!SingleInstance.Start()) { return; } 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     try 
     { 
      var applicationContext = new CustomApplicationContext(); 
      Application.Run(applicationContext); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "Program Terminated Unexpectedly", 
       MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
     SingleInstance.Stop(); 
    } 

CustomApplicationContext.cs

public class CustomApplicationContext : ApplicationContext 
{ 
    private System.ComponentModel.IContainer components; // a list of components to dispose when the context is disposed 
    private NotifyIcon notifyIcon; 
    private static readonly string IconFileName = "green.ico"; 
    private static readonly string DefaultTooltip = "Employee Management System"; 
    private readonly TrayManager trayManager; 

    public CustomApplicationContext() 
    { 
     InitializeContext(); 
     trayManager = new TrayManager(notifyIcon); 
    } 

    protected override void Dispose(bool disposing) 
    { 
     if (disposing && components != null) { components.Dispose(); } 
    } 


    private void InitializeContext() 
    { 
     components = new System.ComponentModel.Container(); 
     notifyIcon = new NotifyIcon(components) 
     { 
      ContextMenuStrip = new ContextMenuStrip(), 
      Icon = new Icon(IconFileName), 
      Text = DefaultTooltip, 
      Visible = true 
     }; 
     notifyIcon.ContextMenuStrip.Opening += ContextMenuStrip_Opening; 
     notifyIcon.DoubleClick += notifyIcon_DoubleClick; 
     //notifyIcon.MouseUp += notifyIcon_MouseUp; 
    } 
private void notifyIcon_DoubleClick(object sender, EventArgs e) 
    { 
     ShowAboutForm(); 
    } 
private TestForm testForm; 

    private void ShowAboutForm() 
    { 
     if (testForm == null) 
     { 
      testForm = new TestForm { trayManager = trayManager }; 
      testForm.Closed += testForm_Closed; ; // avoid reshowing a disposed form 
      testForm.Show(); 
     } 
     else { testForm.Activate(); } 
    } 


    void testForm_Closed(object sender, EventArgs e) 
    { 
     testForm = null; 
    } 

在哪裏添加計時器 - 在語境?用戶可能無法打開表單,因此在表單上添加定時器可能無法一直工作。我如何更改圖標?

+0

只需更改圖標屬性,簡單得不能再簡單或更直觀。 –

回答

6

我會想辦法讓你的圖標嵌入式資源,然後用這樣的代碼來改變在運行時將當前顯示的一個:

notifyIcon.Icon = new Icon(this.GetType(), "red.ico"); 
+0

雖然您的解決方案完全正常,但我認爲使用'Resource.resx'更友好:) +1 –

7

您可以添加2個圖標到Resource.resx文件

this.notifyIcon1.Icon = Properties.Resources.Red; 

:你的項目,Red.ico和Green.ico和在不同情況下使用他們這樣的
this.notifyIcon1.Icon = Properties.Resources.Green; 

將圖標添加到Resourse.resx,打開Resources.resxProperties您project.then的文件夾,從第一個下拉列表中設計師工具欄,選擇Icons,並從下一個下拉菜單中選擇Add Existing File...並添加圖標文件。您也可以在此處重命名項目。

enter image description here enter image description here