WinForm 实现最小化任务栏托盘

步骤:

1. 在Form上添加一个 NotifyIcon 控件,无需设置属性,当时如果想要在最小化到任务栏可以点右键菜单,那还需要添加一个ContextMenuStrip菜单控件,然后把 NotifyIcon的ContextMenuStrip属性设置为菜单控件即可。

2.在 NotifyIcon  的DoubleClick 事件里,添加以下代码:

private void mainNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //双击显示主界面
            if (this.Visible)
            {
                this.WindowState = FormWindowState.Minimized;
                this.mainNotifyIcon.Visible = true;
                this.Hide();
            }
            else
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }
        }

3.在窗体 Form 的Closing 事件添加以下代码:

private void frMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 注意判断关闭事件reason来源于窗体按钮,否则用菜单退出时无法退出!
            if (e.CloseReason == CloseReason.UserClosing)
            {
                //取消"关闭窗口"事件
                e.Cancel = true; // 取消关闭窗体 

                //使关闭时窗口向右下角缩小的效果
                this.WindowState = FormWindowState.Minimized;
                this.mainNotifyIcon.Visible = true;
                //this.m_cartoonForm.CartoonClose();
                this.Hide();
                return;
            } 
        }

这样就搞定了。

转载于:https://www.cnblogs.com/lpq21314/p/9475207.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值