创建任务栏图标
从工具箱中向界面添加
NotifyIcon
控件右键属性设置图标
双击
NotifyIcon
控件,添加隐藏与显示代码:
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.Visible)
{
this.Hide();
}
else
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
}
从工具箱中添加
ContextMenuStrip
控件编辑菜单内容,双击添加事件
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
//this.close();
notifyIcon1.Visible = false;
Environment.Exit(0);
}
private void 隐藏显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.Visible)
{
this.Hide();
}
else
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
}
如果使用了
Form1_FormClosing
事件最小化窗口,则不能使用this.close();
方法关闭程序
关闭时提示
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.notifyIcon1.ShowBalloonTip(3, "提示", "已最小化运行,退出请右键", ToolTipIcon.Info);
e.Cancel = true;
this.Hide();
}