C# WinForm窗口最小化到系统托盘

时间:2023-03-08 17:44:10

* C# WinForm窗口最小化到系统托盘
http://hi.baidu.com/kfxtgtqyapouyze/item/8ccfdcd5a174a7312a35c7c3

主要功能:
(1)、程序启动自动隐藏到任务栏右侧通知栏显示。(与系统托盘同义)
(2)、双击系统托盘图标显示、隐藏窗口;
(3)、右击系统托盘图标提供三个菜单选项,“退出”、“隐藏”、“显示”;

以上(1)的实现:

private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible = true;
}
}

以上(2)的实现:

右击notifyIcon1 ,选择属性,双击其中DoubleClick,添加相关代码

private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
}
else if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
}

单击显示程序界面:

private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}