简体   繁体   中英

Changing System Tray Icon Image

I have built a Tray Application in .Net which works fine. However, users want to change Tray Icon image at runtime on certain conditions. To make it simple, let us say, something is not working - Tray Icon should show Red image; if everything is fine, it should show green. I'm not sure how to achieve this in .Net.

Please provide some inputs on this. Thanks

I built CustomApplicationContent for Tray. Some snippets below:

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;
    }

Where do I add timer - in Context? Users may not open a form so adding timer on Form may not work all the time. How do I change icon?

You can add 2 Icons to Resource.resx file of your project, Red.ico and Green.ico and use them this way in different situations:

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

or

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

To add icons to Resourse.resx , Open Resources.resx from Properties folder of your project.then From first dropdown in toolbar of designer, select Icons , and from the next dropdown select Add Existing File... and add your icon files. You can also rename items here.

在此处输入图片说明 在此处输入图片说明

我会让你的 Icons Embedded Resources ,然后使用这样的代码在运行时更改当前显示的:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM