Changing System Tray Icon Image

这一生的挚爱 提交于 2019-12-07 16:55:53

问题


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?


回答1:


I'd make your Icons Embedded Resources, then use code like this to change the currently displayed one at run-time:

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



回答2:


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.



来源:https://stackoverflow.com/questions/33105307/changing-system-tray-icon-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!