注意:win10系统要打开通知功能
一、添加 NotifyIcon、ContextMenuStrip 控件,并给 NotifyIcon 设置 Icon、Visible 属性
二、在 ContextMenuStrip->Items->MenuItem 控件添加 显示、退出 内容
三、在 NotifyIcon 控件 ContextMenuStrip 属性里绑定添加的 ContextMenuStrip 控件
//窗体关闭在托盘运行并气泡提示
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Visible = false;
notifyIcon1.ShowBalloonTip(3000, "提示", "托盘运行中……", ToolTipIcon.Info);
}
//双击托盘显示窗体
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Visible = true;
//还原窗体
this.WindowState = FormWindowState.Normal;
}
//右键托盘显示窗体
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = true;
//还原窗体
this.WindowState = FormWindowState.Normal;
}
//右键托盘退出程序
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
notifyIcon1.Dispose();
this.Dispose();
Environment.Exit(0);
}
//缩小窗体在托盘运行并气泡提示
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Visible = false;
this.WindowState = FormWindowState.Minimized;
notifyIcon1.ShowBalloonTip(3000, "提示", "托盘运行中……", ToolTipIcon.Info);
}
}
最终效果: