更换系统托盘图标图像 [英] Changing System Tray Icon Image

查看:179
本文介绍了更换系统托盘图标图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在.Net中构建了一个纸盘应用程序,该应用程序可以正常工作.但是,用户希望在某些情况下在运行时更改任务栏图标图​​像.为了简单起见,让我们说,有些事情不起作用-托盘图标应显示为红色图像;如果一切正常,则应显示绿色.我不确定如何在.Net中实现这一目标.

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

我为纸盘构建了CustomApplicationContent.以下是一些摘要:

I built CustomApplicationContent for Tray. Some snippets below:

Program.cs

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

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?

推荐答案

我将使您的图标成为嵌入式资源,然后使用类似这样的代码在运行时更改当前显示的图标:

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");

这篇关于更换系统托盘图标图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆