WPF托盘控件

什么是托盘控件?
相信大家看到下面这个图片,就大概清楚了,没错,就是Windows窗体右下角的图标栏。

在这里插入图片描述

1、在App.xmal中添加

 ShutdownMode="OnExplicitShutdown" Startup="ApplicationStartup" Exit="ApplicationExit">

2、在App.xmal.cs中添加命名空间

using System.Windows;
using System.Windows.Forms;
using Application = System.Windows.Application;

3、在App.xmal.cs中添加代码

/// <summary>
        /// 开始运行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ApplicationStartup(object sender,StartupEventArgs e)
        {
            AddTrayIcon();
            //RemoveTrayIcon();
        }


        /// <summary>
        /// 添加基础信息
        /// </summary>
        private void AddTrayIcon()
        {
            if (trayIcon!=null)
            {
                return;
            }
            trayIcon = new NotifyIcon
            {
                Icon = new System.Drawing.Icon("托盘图标.ico"),//图标图片
                Text = "智晟网络会所"//冒泡内容
            };
            trayIcon.Visible = true;



            #region 添加右键菜单内容
            //实例化右键菜单
            ContextMenu menu = new ContextMenu();

            //添加菜单的内容
            MenuItem OnlineItem = new MenuItem();
            OnlineItem.Text = "查看上机记录";
            OnlineItem.Click += new EventHandler(OnlineNote);

            MenuItem ReChargeItem = new MenuItem();
            ReChargeItem.Text = "查看充值记录";
            ReChargeItem.Click += new EventHandler(ReChargeNote);

            MenuItem ModigyPWD = new MenuItem();
            ModigyPWD.Text = "修改密码";
            ModigyPWD.Click += new EventHandler(ModifyPwd);

            //把写好的内容加进去
            menu.MenuItems.Add(ReChargeItem);
            menu.MenuItems.Add(OnlineItem);
            menu.MenuItems.Add(ModigyPWD);

            trayIcon.ContextMenu = menu;//设置右键弹出菜单            #endregion
            #endregion
        }


        /// <summary>
        /// 结束时调用的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ApplicationExit(object sender,ExitEventArgs e)
        {
            RemoveTrayIcon();
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        private void RemoveTrayIcon()
        {
            if (trayIcon != null)
            {
                trayIcon.Visible = false;
                trayIcon.Dispose();//释放资源
                trayIcon = null;
            }
        }

        /// <summary>
        /// 查询上机记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnlineNote(object sender, EventArgs e)
        {
            #region 单例模式
            OnlineNoteUI OnlineNote = OnlineNoteUI.GetInstance();
            OnlineNote.Show();
            #endregion

        }



        /// <summary>
        /// 双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnNotifyIconDoubleClick(object sender,EventArgs e)
        {
            
        }

        /// <summary>
        /// 查询充值记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReChargeNote(object sender, EventArgs e)
        {
            //单例模式
            ReChargeNoteUI ReChargeNote =ReChargeNoteUI.GetInstance();
            ReChargeNote.Show();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ModifyPwd(object sender, EventArgs e)
        {
            //单例模式
            ModifyPwdUI ModifyPwd = ModifyPwdUI.GetInStance();
            ModifyPwd.Show();
        }

总结一下,希望对大家有所帮助!

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值