更改系统托盘图标图片

5
我已经在.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;
    }

我应该在哪里添加计时器 - 在上下文中吗?因为用户可能不会打开表单,所以在表单上添加计时器可能并不总是有效。我如何改变图标?


只需更改图标属性,就可以更简单和直观了。 - Hans Passant
2个回答

19
你可以在项目的 Resource.resx 文件中添加 2 个图标,Red.ico 和 Green.ico,并在不同情况下使用它们:
this.notifyIcon1.Icon = Properties.Resources.Red;

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

要将图标添加到Resourse.resx,请从项目的Properties文件夹中打开Resources.resx。然后从设计器工具栏中的第一个下拉菜单中选择Icons,从下一个下拉菜单中选择Add Existing File...并添加您的图标文件。您也可以在此处重命名项目。

enter image description here enter image description here


7

我建议您将图标作为嵌入式资源,然后使用以下代码在运行时更改当前显示的图标:

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

虽然你的解决方案完全没问题,但我认为使用 Resource.resx 更加友好 :) +1 - Reza Aghaei

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接