WinForm窗口最小化到托盘

先给窗口添加两个组件: notifyIcon 和 contextMenuStrip
并且把contextMenuStrip 绑定到 notifyIcon 上面
再给contextMenuStrip添加两个子菜单: 恢复 和 退出
然后依次实现下面的事件


        private void Form1_Load(object sender, EventArgs e)
        {
            int x = Screen.PrimaryScreen.WorkingArea.Right - this.Width - 10;
            int y = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
            this.Location = new Point(x, y);//设置窗体在屏幕右下角显示
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = false;
            if (Screen.PrimaryScreen.WorkingArea.Right - this.Right < 3)
            {
                this.Left = Screen.PrimaryScreen.WorkingArea.Right - this.Width - 10;
            }

            if (this.Top < 3)
            {
                this.Top = 5;
            }
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = false;
            }
        }

        private bool _isClose = false;

        private void 恢复ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            notifyIcon1_DoubleClick(sender, null);
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _isClose = true;
            this.notifyIcon1.Visible = false;
            Application.Exit();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!_isClose)
            {
                e.Cancel = true;
            }
            this.Visible = false;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值