更改系统托盘图标图像
Changing System Tray Icon Image
我在 .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;
}
我在哪里添加计时器 - 在 Context 中?用户可能不会打开表单,因此在表单上添加计时器可能不会一直有效。如何更改图标?
我会制作您的图标 嵌入式资源,然后使用这样的代码在 运行 时更改当前显示的图标:
notifyIcon.Icon = new Icon(this.GetType(), "red.ico");
您可以在项目的Resource.resx文件中添加2个图标,Red.ico和Green.ico并在不同情况下以这种方式使用它们:
this.notifyIcon1.Icon = Properties.Resources.Red;
或
this.notifyIcon1.Icon = Properties.Resources.Green;
要将图标添加到 Resourse.resx
,请从 project.then 的 Properties
文件夹中打开 Resources.resx
从设计器工具栏的第一个下拉列表中,select Icons
,然后从下一个下拉列表 select Add Existing File...
中添加您的图标文件。您也可以在此处重命名项目。
我在 .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;
}
我在哪里添加计时器 - 在 Context 中?用户可能不会打开表单,因此在表单上添加计时器可能不会一直有效。如何更改图标?
我会制作您的图标 嵌入式资源,然后使用这样的代码在 运行 时更改当前显示的图标:
notifyIcon.Icon = new Icon(this.GetType(), "red.ico");
您可以在项目的Resource.resx文件中添加2个图标,Red.ico和Green.ico并在不同情况下以这种方式使用它们:
this.notifyIcon1.Icon = Properties.Resources.Red;
或
this.notifyIcon1.Icon = Properties.Resources.Green;
要将图标添加到 Resourse.resx
,请从 project.then 的 Properties
文件夹中打开 Resources.resx
从设计器工具栏的第一个下拉列表中,select Icons
,然后从下一个下拉列表 select Add Existing File...
中添加您的图标文件。您也可以在此处重命名项目。