1、在应用程序主界面添加控件notifyIcon1,并设置相关属性,BalloonTipIcon,BalloonTipText,BalloonTipTitle,Icon,Text,Tag,Visible=False等。
2、在MouseClick或MouseDoubleClick事件中添加代码:
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
notifyIcon1.Visible = false;
}
3、添加全局变量private Boolean IsExit,并在FormLoad事件中设置IsExit = false;
4、在FormClosing事件中判断IsExit,决定是否退出
private void frm_Main_FormClosing(object sender, FormClosingEventArgs e)
{
if (!IsExit)
{
this.Hide();
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(2000);
e.Cancel = true;
}
else
{
try { Application.Exit(); }
catch { }
}
}
5、在界面上添加退出按钮,在Click事件中添加代码
private void btn_Exit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("您确认要退出*****系统吗?", "警告", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
IsExit = true;
this.Close();
}
}