加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 更改系统托盘图标图像

发布时间:2020-12-15 19:40:06 所属栏目:百科 来源:网络整理
导读:我在.Net中构建了一个托盘应用程序,工作正常.但是,用户希望在某些条件下在运行时更改“托盘图标”图像.为了简单起见,让我们说,有些东西不起作用 – 托盘图标应显示红色图像;如果一切都很好,它应该显示绿色.我不知道如何在.Net中实现这一目标. 请提供一些意见
我在.Net中构建了一个托盘应用程序,工作正常.但是,用户希望在某些条件下在运行时更改“托盘图标”图像.为了简单起见,让我们说,有些东西不起作用 – 托盘图标应显示红色图像;如果一切都很好,它应该显示绿色.我不知道如何在.Net中实现这一目标.

请提供一些意见.谢谢

我为Tray构建了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;
    }

我在哪里添加计时器 – 在上下文中?用户可能无法打开表单,因此在表单上添加计时器可能无法一直运行.如何更改图标?

解决方法

我将创建您的Icons Embedded Resources,然后使用这样的代码在运行时更改当前显示的代码:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读