最小化窗体时,托盘显示窗体小图标
鼠标右击出现窗体操作
窗体最小化和关闭窗体都会进入托盘管理只有托盘右击退出才会真正关闭运行程序
完整代码:
namespace Day10_Test
{
public partial class Form1 : Form
{
private bool isExit = false;
public Form1()
{
InitializeComponent();
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(2000, notifyIcon1.BalloonTipTitle, notifyIcon1.BalloonTipText, ToolTipIcon.Info);
}
}
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
//notifyIcon1.Visible=true;
Show();
WindowState = FormWindowState.Normal;
}
private void 胡ToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("你确定要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
isExit = true;
this.Close();
Application.Exit();
}
}
//窗体关闭前的动作
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing && isExit == false)
{
e.Cancel = true;//取消关闭窗体动作
this.Hide();
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(1000, notifyIcon1.BalloonTipTitle, notifyIcon1.BalloonTipText, ToolTipIcon.Info);
}
}
}
}