简体   繁体   English

更改系统托盘图标图像

[英]Changing System Tray Icon Image

I have built a Tray Application in .Net which works fine.我在 .Net 中构建了一个托盘应用程序,它工作正常。 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.我不确定如何在 .Net 中实现这一点。

Please provide some inputs on this.请就此提供一些意见。 Thanks谢谢

I built CustomApplicationContent for Tray.我为 Tray 构建了 CustomApplicationContent。 Some snippets below:下面的一些片段:

Program.cs程序.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自定义应用程序上下文.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:您可以在项目的Resource.resx文件 Red.ico 和 Green.ico 中添加 2 个图标,并在不同情况下以这种方式使用它们:

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.要将图标添加到Resourse.resx ,请从项目的Properties文件夹中打开Resources.resx从设计器工具栏中的第一个下拉列表中,选择Icons ,然后从下一个下拉列表中选择Add Existing File...并添加您的图标文件。 You can also rename items here.您还可以在此处重命名项目。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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