作用:系统托盘图标控件,用于应用程序最小化或后台运行时在托盘上面显示图标。
常用属性:
对象名称name,icon图标,右键菜单contextMenuStrip
常用事件:
后台代码示范:
NotifyIcon、contextMenuStrip添加如下:
//关闭窗口时,不退出程序,使程序最小化至托盘
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();
}