[WPF]创建系统栏小图标

隐藏窗体任务栏图标

/// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            this.ShowInTaskbar = false;
        }
    }

 

 

系统图标类 

  public class SystemTray
    {
        public static SystemTray Instance;
        /// <summary>
        /// 静态构造函数,在类第一次被创建或者静态成员被调用的时候调用
        /// </summary>
        static SystemTray()
        {
            Instance=new SystemTray();
        }

        public NotifyIcon Ni { get; set; }

        private SystemTray()
        {
            Ni = new System.Windows.Forms.NotifyIcon();
            Stream iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/StikyNotes;component/MyLogo.ico")).Stream;
            Ni.Icon = new System.Drawing.Icon(iconStream);
            Ni.Visible = true;
            Ni.MouseClick += Ni_MouseClick;
        }

        private void Ni_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                System.Windows.Controls.ContextMenu NotifyIconMenu = (System.Windows.Controls.ContextMenu)App.Current.FindResource("NotifyIconMenu");
                NotifyIconMenu.IsOpen = true;
                App.Current.MainWindow?.Activate();
            }

            if (e.Button == MouseButtons.Left)
            {
                var wins=WindowsManager.Instance.Windows;
                foreach (var i in wins)
                {
                    i.Activate();
                }
            }
        }

        /// <summary>
        /// 销毁系统托盘图标的资源
        /// </summary>
        public void DisposeNotifyIcon()
        {
            Ni?.Dispose();
        }
    }

 

在App.xaml中:

 public partial class App : Application
    {
        System.Threading.Mutex mutex;
        protected override void OnStartup(StartupEventArgs e)
        {
            bool ret;
            mutex = new System.Threading.Mutex(true, "StikyNotesAPP", out ret);

            if (!ret)
            {
                MessageBox.Show("程序已经运行了");
                Environment.Exit(0);
            }

            base.OnStartup(e);
            var systemtray = SystemTray.Instance;
        }

        protected override void OnDeactivated(EventArgs e)
        {
            base.OnDeactivated(e);
            System.Windows.Controls.ContextMenu menu = this.FindResource("NotifyIconMenu") as ContextMenu;
            if (menu.IsOpen == true)
            {
                menu.IsOpen = false;
            }

        }



        /// <summary>
        /// 新建窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuOpen_Click(object sender, RoutedEventArgs e)
        {
            var MainWindow = new MainWindow();
            MainWindow.DataContext = new MainViewModel(MainWindow, new WindowsData());
            WindowsManager.Instance.Windows.Add(MainWindow);
            MainWindow.Show();
        }

        /// <summary>
        /// 窗体退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuExit_Click(object sender, RoutedEventArgs e)
        {
            SystemTray.Instance.DisposeNotifyIcon();
            App.Current.Shutdown();
        }
        /// <summary>
        /// 窗体退出事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            XMLHelper.SaveObjAsXml(ProgramData.Instance, "StikyNotesData.xml");
            SystemTray.Instance.DisposeNotifyIcon();

        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            var programData = XMLHelper.DecodeXML<ProgramData>("StikyNotesData.xml");
            if (programData != null)
            {
                var windowsDatas = programData.Datas;
                ProgramData.Instance.IsWindowTopMost = programData.IsWindowTopMost;
                ProgramData.Instance.IsStartUpWithSystem = programData.IsStartUpWithSystem;
                MainWindow MainWindow;
                //有创建过的窗口
                if (windowsDatas.Count > 0)
                {
                    for (int i = 0; i < windowsDatas.Count; i++)
                    {
                        MainWindow = new MainWindow();
                        MainWindow.DataContext = new MainViewModel(MainWindow, windowsDatas[i]);
                        MainWindow.Show();
                    }
                }
                else//以前的窗口都被删掉了
                {
                    MainWindow = new MainWindow();
                    MainWindow.DataContext = new MainViewModel(MainWindow, new WindowsData());
                    MainWindow.Show();
                }
            }
            //没有创建过的窗口
            else
            {
                var MainWindow = new MainWindow();
                MainWindow.DataContext = new MainViewModel(MainWindow, new WindowsData());
                MainWindow.Show();
            }
        }
    }

App.xaml中
 

 <ContextMenu x:Key="NotifyIconMenu" StaysOpen="False">
                <MenuItem
                    Name="MenuOpen"
                    Click="MenuOpen_Click"
                    Header="新建" />
                <MenuItem
                    Name="MenuExit"
                    Click="MenuExit_Click"
                    Header="退出" />
            </ContextMenu>

问题

 记得在退出的时候要释放使用的图标资源,否则在程序退出之后,系统栏中还会存在小图标,要使用下面这条语句:

SystemTray.Instance.DisposeNotifyIcon();

 

效果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值