关闭窗体执行托盘操作

 托盘其实只要把一个你设计好的图标放到属性“ICON”以外,别的就交给代码来完成了。

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel 
= true// 取消关闭窗体 
            this.Hide();
            
this.ShowInTaskbar=false;
            
this.notifyIcon1.Visible = true;//显示托盘图标 
        }

 

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

我相信代码都能看懂吧,接下来就是右键菜单的操作了。其实系统提供了一个菜单操作,我不太喜欢用,我感觉还是下面的比较直接点。


        
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
this.Close();
            
this.notifyIcon1.Visible = false;
            Application.Exit(); 
        }
可能大家要问一个this.close();不就可以了吗,为什么要那么多的代码来退出那。其实光一个CLOSE是退出不了程序的,不信你可以在 e.Cancel = true;设个段点看看就知道了,当你执行CLOSE的时候,程序又回到e.Cancel这里来了,所以我们要用关闭main()来关闭程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值