2015-10-13 173 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