博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.net窗口最小化 解决方案

Posted on 2022-05-12 08:17  myzerg  阅读(92)  评论(0编辑  收藏  举报

1 form 上 拖拽放上 notifyIcon 控件 和 contextMenuStrip 控件

2 notifyIcon 设定 ico 属性 和 contextMenuStrip 属性

3 程序选择一个图标,一定要有图标 ico文件
4 FormClosing 事件里 写如下代码,保证form 不会被关闭只会被最小化
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;

Form 的 SizeChanged 事件里 写如下代码
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible = true;
}

 

4 contextMenuStrip 增加两个 菜单项分别位 open 和 exit

open 的代码如下
this.Show();
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
this.Visible = true;
//this.Activate();
this.notifyIcon1.Visible = false;

exit 代码如下

this.notifyIcon1.Visible = false;
Application.Exit();
//自己释放特殊的变量,这个退出很不科学