Function: System tray icon control, used to display icons on the tray when the application is minimized or running in the background.
Common properties:
Object name name, icon icon, right-click menu contextMenuStrip
Common events:
Backend code demonstration:
NotifyIcon and contextMenuStrip are added as follows:
//关闭窗口时,不退出程序,使程序最小化至托盘
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.ApplicationExitCall)
{
Application.Exit();
}
else
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.Hide();
this.notifyIcon1.Visible = true;
}
}
//托盘图标的鼠标双击图标事件
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
notifyIcon1.Visible = true;
this.Show();
this.Activate();
this.WindowState = FormWindowState.Normal;
}
//右键菜单之显示: 用在托盘图标上面
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
//右键菜单之隐藏: 用在托盘图标上面
private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
}
//右键菜单之退出: 用在托盘图标上面
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}