我在.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;
}
在哪裏添加計時器 - 在語境?用戶可能無法打開表單,因此在表單上添加定時器可能無法一直工作。我如何更改圖標?
只需更改圖標屬性,簡單得不能再簡單或更直觀。 –