C#:实现托盘

 

1、向窗体上添加如下控件:MenuStrip menuStrip1, NotifyIcon ni_frmMain,Timer timer1, ContentMenuStrip cms_notify。其中notify中包含显示、退出等。

2、实现的代码:

        #region 托盘相关代码

        #region 私有方法 处理窗体的 显示 隐藏 关闭(退出)

        /// <summary>
        /// 显示
        /// </summary>
        private void ShowMainForm()
        {
            this.Show();
            this.WindowState = this.lastFormState;
            this.Activate();

            this.ShowInTaskbar = true;  //任务栏中显示窗体图标
            //this.ni_frmMain.Visible = false;//图盘中隐藏图标
        }

        /// <summary>
        /// 隐藏
        /// </summary>
        private void HideMainForm()
        {
            this.Hide();

            this.ShowInTaskbar = false;  //任务栏中显示窗体图标
            //this.ni_frmMain.Visible = true;//图盘中隐藏图标
        }

        /// <summary>
        /// 关闭(退出)
        /// </summary>
        private void ExitMainForm()
        {
            if (MessageBox.Show("您确定要退出主程序吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                this.ni_frmMain.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }
        }

        #endregion

        #region 右键菜单处理,显示 隐藏 退出

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmi_notifyShow_Click(object sender, EventArgs e)
        {
            ShowMainForm();
        }

        /// <summary>
        /// 隐藏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmi_notifyHide_Click(object sender, EventArgs e)
        {
            HideMainForm();
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmi_notifyExit_Click(object sender, EventArgs e)
        {
            ExitMainForm();
        }

        #endregion

        #region 私有方法 双击托盘上图标时,显示窗体

        private void ni_frmMain_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                ShowMainForm();
            }
            else
            {
                this.WindowState = FormWindowState.Minimized;
                HideMainForm();
            }
        }


        #endregion

        #region 点最小化按钮时,最小化到托盘

        private void frmMain_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Minimized)
            {
                this.ShowInTaskbar = true;  //任务栏中窗体图标显示
                //this.ni_frmMain.Visible = false;
                this.lastFormState = this.WindowState;
            }
            else
            {
                HideMainForm();
            }
        }


        #endregion

        #region 窗体关闭时最小化到托盘
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //e.Cancel = true;
            //this.ShowInTaskbar = false;  //任务栏中窗体图标消失
            //this.ni_frmMain.Visible = true;
            //HideTipForm();
        }

        #endregion


        #region 图标闪烁 开启 关闭

        /// <summary>
        /// 开启
        /// </summary>
        private void StartFlicker()
        {
            this.timer1.Enabled = true;
            this.timer1.Start();
        }

        /// <summary>
        /// 关闭
        /// </summary>
        private void CloseFlicker()
        {
            this.timer1.Stop();
            this.timer1.Enabled = false;
            this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore0;
        }

        private int i = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (i < 1)
            {
                this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore0;
                i++;
                return;
            }
            else
            {
                this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore1;
                i = 0;
            }
        }
        #endregion


        #endregion
View Code

 

转载于:https://www.cnblogs.com/shenchao/p/4726427.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值