系统托盘图标

本文介绍了一个使用C#编写的简单窗体应用程序实例,包括最大化窗口、最小化到系统托盘、从托盘恢复显示及安全退出等功能。通过几个关键事件处理展示了如何控制窗体的状态变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个菜单栏

一个按钮


    public partial class FrmMain : Form
    {
        bool isExit = false;

        public FrmMain()
        {
            InitializeComponent();
        }

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

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!isExit)
            {
                e.Cancel = true;
                this.Hide();
                this.tool_exit.PerformClick(); //默认是最小化
            }

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Maximized;

        }

        private void ContextM_open_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Maximized;

        }

        private void contextM_exit_Click(object sender, EventArgs e)
        {
            // ***************************************************************************************
            string message = "你确定退出本程序?";
            string caption = "提示信息!";
            MessageBoxIcon msgBoxIcon = MessageBoxIcon.Information;
            MessageBoxButtons msgBtn = MessageBoxButtons.OKCancel;
            DialogResult result = MessageBox.Show(message, caption, msgBtn, msgBoxIcon);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                isExit = true;
                Application.Exit();
            }
 

        }

        private void tool_exit_Click(object sender, EventArgs e)//仅托盘图标,默认退出要点击右键
        {
            if (!isExit)
            {
                // ***************************************************************************************
                this.Hide();
                //notifyIcon1.Icon = this.Icon;
                notifyIcon1.Text = "计划任务执行";
                this.notifyIcon1.Visible = true;
                this.notifyIcon1.ShowBalloonTip(5000, "提示", "系统仍在运行!", ToolTipIcon.Info);

                // ***************************************************************************************
            }



        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值