首先, 要确定已经将控件NotifyIcon拖到应用窗体中,属性name为notifyicon1.
然后, 双击窗体的FormClosing的事件 ,具码内容如下:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("是否退出?选否,最小化到托盘", "操作提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
this.Dispose();
}
else
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.Visible = false;
this.notifyIcon1.Visible = true;
}
}
这样, 这一简单而又实用的功能就完成了.